]>
Commit | Line | Data |
---|---|---|
65ce860e | 1 | ## @file\r |
9b98c416 | 2 | # This tool encodes and decodes GUIDed FFS sections or FMP capsule for a GUID type of\r |
65ce860e MK |
3 | # EFI_CERT_TYPE_RSA2048_SHA256_GUID defined in the UEFI 2.4 Specification as\r |
4 | # {0xa7717414, 0xc616, 0x4977, {0x94, 0x20, 0x84, 0x47, 0x12, 0xa7, 0x35, 0xbf}}\r | |
5 | # This tool has been tested with OpenSSL 1.0.1e 11 Feb 2013\r | |
6 | #\r | |
8a0933f4 | 7 | # Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>\r |
65ce860e MK |
8 | # This program and the accompanying materials\r |
9 | # are licensed and made available under the terms and conditions of the BSD License\r | |
10 | # which accompanies this distribution. The full text of the license may be found at\r | |
11 | # http://opensource.org/licenses/bsd-license.php\r | |
12 | #\r | |
13 | # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
14 | # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
15 | #\r | |
16 | \r | |
17 | '''\r | |
18 | Rsa2048Sha256Sign\r | |
19 | '''\r | |
20 | \r | |
21 | import os\r | |
22 | import sys\r | |
23 | import argparse \r | |
24 | import subprocess\r | |
25 | import uuid\r | |
26 | import struct\r | |
27 | import collections\r | |
c9df168f MK |
28 | from Common.BuildVersion import gBUILD_VERSION\r |
29 | \r | |
30 | #\r | |
31 | # Globals for help information\r | |
32 | #\r | |
33 | __prog__ = 'Rsa2048Sha256Sign'\r | |
34 | __version__ = '%s Version %s' % (__prog__, '0.9 ' + gBUILD_VERSION)\r | |
9b98c416 | 35 | __copyright__ = 'Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.'\r |
c9df168f | 36 | __usage__ = '%s -e|-d [options] <input_file>' % (__prog__)\r |
65ce860e MK |
37 | \r |
38 | #\r | |
39 | # GUID for SHA 256 Hash Algorithm from UEFI Specification\r | |
40 | #\r | |
41 | EFI_HASH_ALGORITHM_SHA256_GUID = uuid.UUID('{51aa59de-fdf2-4ea3-bc63-875fb7842ee9}')\r | |
42 | \r | |
43 | #\r | |
44 | # Structure defintion to unpack EFI_CERT_BLOCK_RSA_2048_SHA256 from UEFI 2.4 Specification\r | |
45 | #\r | |
46 | # typedef struct _EFI_CERT_BLOCK_RSA_2048_SHA256 {\r | |
47 | # EFI_GUID HashType;\r | |
48 | # UINT8 PublicKey[256];\r | |
49 | # UINT8 Signature[256];\r | |
50 | # } EFI_CERT_BLOCK_RSA_2048_SHA256;\r | |
51 | #\r | |
52 | EFI_CERT_BLOCK_RSA_2048_SHA256 = collections.namedtuple('EFI_CERT_BLOCK_RSA_2048_SHA256', ['HashType','PublicKey','Signature'])\r | |
53 | EFI_CERT_BLOCK_RSA_2048_SHA256_STRUCT = struct.Struct('16s256s256s')\r | |
54 | \r | |
55 | #\r | |
56 | # Filename of test signing private key that is stored in same directory as this tool\r | |
57 | #\r | |
58 | TEST_SIGNING_PRIVATE_KEY_FILENAME = 'TestSigningPrivateKey.pem'\r | |
59 | \r | |
60 | if __name__ == '__main__':\r | |
65ce860e MK |
61 | #\r |
62 | # Create command line argument parser object\r | |
63 | # \r | |
c9df168f | 64 | parser = argparse.ArgumentParser(prog=__prog__, version=__version__, usage=__usage__, description=__copyright__, conflict_handler='resolve')\r |
65ce860e MK |
65 | group = parser.add_mutually_exclusive_group(required=True)\r |
66 | group.add_argument("-e", action="store_true", dest='Encode', help='encode file')\r | |
67 | group.add_argument("-d", action="store_true", dest='Decode', help='decode file')\r | |
b40286bb | 68 | parser.add_argument("-o", "--output", dest='OutputFile', type=str, metavar='filename', help="specify the output filename", required=True)\r |
9b98c416 | 69 | parser.add_argument("--monotonic-count", dest='MonotonicCountStr', type=str, help="specify the MonotonicCount in FMP capsule.")\r |
65ce860e MK |
70 | parser.add_argument("--private-key", dest='PrivateKeyFile', type=argparse.FileType('rb'), help="specify the private key filename. If not specified, a test signing key is used.")\r |
71 | parser.add_argument("-v", "--verbose", dest='Verbose', action="store_true", help="increase output messages")\r | |
72 | parser.add_argument("-q", "--quiet", dest='Quiet', action="store_true", help="reduce output messages")\r | |
73 | parser.add_argument("--debug", dest='Debug', type=int, metavar='[0-9]', choices=range(0,10), default=0, help="set debug level")\r | |
65ce860e MK |
74 | parser.add_argument(metavar="input_file", dest='InputFile', type=argparse.FileType('rb'), help="specify the input filename")\r |
75 | \r | |
76 | #\r | |
77 | # Parse command line arguments\r | |
78 | # \r | |
79 | args = parser.parse_args()\r | |
80 | \r | |
81 | #\r | |
82 | # Generate file path to Open SSL command\r | |
83 | #\r | |
84 | OpenSslCommand = 'openssl'\r | |
85 | try:\r | |
86 | OpenSslPath = os.environ['OPENSSL_PATH']\r | |
87 | OpenSslCommand = os.path.join(OpenSslPath, OpenSslCommand)\r | |
a3607b26 YZ |
88 | if ' ' in OpenSslCommand:\r |
89 | OpenSslCommand = '"' + OpenSslCommand + '"'\r | |
65ce860e MK |
90 | except:\r |
91 | pass\r | |
92 | \r | |
93 | #\r | |
94 | # Verify that Open SSL command is available\r | |
95 | #\r | |
96 | try:\r | |
8a0933f4 | 97 | Process = subprocess.Popen('%s version' % (OpenSslCommand), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r |
65ce860e MK |
98 | except: \r |
99 | print 'ERROR: Open SSL command not available. Please verify PATH or set OPENSSL_PATH'\r | |
100 | sys.exit(1)\r | |
101 | \r | |
102 | Version = Process.communicate()\r | |
103 | if Process.returncode <> 0:\r | |
104 | print 'ERROR: Open SSL command not available. Please verify PATH or set OPENSSL_PATH'\r | |
105 | sys.exit(Process.returncode)\r | |
106 | print Version[0]\r | |
107 | \r | |
108 | #\r | |
109 | # Read input file into a buffer and save input filename\r | |
110 | # \r | |
111 | args.InputFileName = args.InputFile.name\r | |
112 | args.InputFileBuffer = args.InputFile.read()\r | |
113 | args.InputFile.close()\r | |
114 | \r | |
115 | #\r | |
b40286bb | 116 | # Save output filename and check if path exists\r |
65ce860e | 117 | #\r |
b40286bb YL |
118 | OutputDir = os.path.dirname(args.OutputFile)\r |
119 | if not os.path.exists(OutputDir):\r | |
120 | print 'ERROR: The output path does not exist: %s' % OutputDir\r | |
121 | sys.exit(1)\r | |
122 | args.OutputFileName = args.OutputFile\r | |
65ce860e MK |
123 | \r |
124 | #\r | |
125 | # Save private key filename and close private key file\r | |
126 | #\r | |
127 | try:\r | |
128 | args.PrivateKeyFileName = args.PrivateKeyFile.name\r | |
129 | args.PrivateKeyFile.close()\r | |
130 | except:\r | |
131 | try:\r | |
132 | #\r | |
133 | # Get path to currently executing script or executable\r | |
134 | #\r | |
135 | if hasattr(sys, 'frozen'):\r | |
136 | RsaToolPath = sys.executable\r | |
137 | else:\r | |
138 | RsaToolPath = sys.argv[0]\r | |
139 | if RsaToolPath.startswith('"'):\r | |
140 | RsaToolPath = RsaToolPath[1:]\r | |
141 | if RsaToolPath.endswith('"'):\r | |
142 | RsaToolPath = RsaToolPath[:-1]\r | |
143 | args.PrivateKeyFileName = os.path.join(os.path.dirname(os.path.realpath(RsaToolPath)), TEST_SIGNING_PRIVATE_KEY_FILENAME)\r | |
144 | args.PrivateKeyFile = open(args.PrivateKeyFileName, 'rb')\r | |
145 | args.PrivateKeyFile.close()\r | |
146 | except:\r | |
147 | print 'ERROR: test signing private key file %s missing' % (args.PrivateKeyFileName)\r | |
148 | sys.exit(1)\r | |
149 | \r | |
150 | #\r | |
151 | # Extract public key from private key into STDOUT\r | |
152 | #\r | |
a5f26fef | 153 | Process = subprocess.Popen('%s rsa -in "%s" -modulus -noout' % (OpenSslCommand, args.PrivateKeyFileName), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r |
65ce860e MK |
154 | PublicKeyHexString = Process.communicate()[0].split('=')[1].strip()\r |
155 | PublicKey = ''\r | |
156 | while len(PublicKeyHexString) > 0:\r | |
157 | PublicKey = PublicKey + chr(int(PublicKeyHexString[0:2],16))\r | |
158 | PublicKeyHexString=PublicKeyHexString[2:]\r | |
159 | if Process.returncode <> 0:\r | |
160 | sys.exit(Process.returncode)\r | |
9b98c416 YZ |
161 | \r |
162 | if args.MonotonicCountStr:\r | |
163 | try:\r | |
164 | if args.MonotonicCountStr.upper().startswith('0X'):\r | |
165 | args.MonotonicCountValue = (long)(args.MonotonicCountStr, 16)\r | |
166 | else:\r | |
167 | args.MonotonicCountValue = (long)(args.MonotonicCountStr)\r | |
168 | except:\r | |
169 | pass\r | |
170 | \r | |
65ce860e | 171 | if args.Encode:\r |
9b98c416 YZ |
172 | FullInputFileBuffer = args.InputFileBuffer\r |
173 | if args.MonotonicCountStr:\r | |
245cda66 YZ |
174 | format = "%dsQ" % len(args.InputFileBuffer)\r |
175 | FullInputFileBuffer = struct.pack(format, args.InputFileBuffer, args.MonotonicCountValue)\r | |
65ce860e MK |
176 | # \r |
177 | # Sign the input file using the specified private key and capture signature from STDOUT\r | |
178 | #\r | |
a5f26fef | 179 | Process = subprocess.Popen('%s sha256 -sign "%s"' % (OpenSslCommand, args.PrivateKeyFileName), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r |
9b98c416 | 180 | Signature = Process.communicate(input=FullInputFileBuffer)[0]\r |
65ce860e MK |
181 | if Process.returncode <> 0:\r |
182 | sys.exit(Process.returncode)\r | |
183 | \r | |
184 | #\r | |
185 | # Write output file that contains hash GUID, Public Key, Signature, and Input data\r | |
186 | # \r | |
187 | args.OutputFile = open(args.OutputFileName, 'wb')\r | |
188 | args.OutputFile.write(EFI_HASH_ALGORITHM_SHA256_GUID.get_bytes_le())\r | |
189 | args.OutputFile.write(PublicKey)\r | |
190 | args.OutputFile.write(Signature)\r | |
191 | args.OutputFile.write(args.InputFileBuffer)\r | |
192 | args.OutputFile.close()\r | |
193 | \r | |
194 | if args.Decode:\r | |
195 | #\r | |
196 | # Parse Hash Type, Public Key, and Signature from the section header\r | |
197 | #\r | |
198 | Header = EFI_CERT_BLOCK_RSA_2048_SHA256._make(EFI_CERT_BLOCK_RSA_2048_SHA256_STRUCT.unpack_from(args.InputFileBuffer))\r | |
199 | args.InputFileBuffer = args.InputFileBuffer[EFI_CERT_BLOCK_RSA_2048_SHA256_STRUCT.size:]\r | |
200 | \r | |
201 | #\r | |
202 | # Verify that the Hash Type matches the expected SHA256 type\r | |
203 | #\r | |
204 | if uuid.UUID(bytes_le = Header.HashType) <> EFI_HASH_ALGORITHM_SHA256_GUID:\r | |
205 | print 'ERROR: unsupport hash GUID'\r | |
206 | sys.exit(1)\r | |
207 | \r | |
208 | #\r | |
209 | # Verify the public key\r | |
210 | #\r | |
211 | if Header.PublicKey <> PublicKey:\r | |
212 | print 'ERROR: Public key in input file does not match public key from private key file'\r | |
213 | sys.exit(1)\r | |
214 | \r | |
9b98c416 YZ |
215 | FullInputFileBuffer = args.InputFileBuffer\r |
216 | if args.MonotonicCountStr:\r | |
245cda66 YZ |
217 | format = "%dsQ" % len(args.InputFileBuffer)\r |
218 | FullInputFileBuffer = struct.pack(format, args.InputFileBuffer, args.MonotonicCountValue)\r | |
9b98c416 | 219 | \r |
65ce860e MK |
220 | #\r |
221 | # Write Signature to output file\r | |
222 | #\r | |
223 | open(args.OutputFileName, 'wb').write(Header.Signature)\r | |
224 | \r | |
225 | #\r | |
226 | # Verify signature\r | |
227 | # \r | |
a5f26fef | 228 | Process = subprocess.Popen('%s sha256 -prverify "%s" -signature %s' % (OpenSslCommand, args.PrivateKeyFileName, args.OutputFileName), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r |
9b98c416 | 229 | Process.communicate(input=FullInputFileBuffer)\r |
65ce860e MK |
230 | if Process.returncode <> 0:\r |
231 | print 'ERROR: Verification failed'\r | |
232 | os.remove (args.OutputFileName)\r | |
233 | sys.exit(Process.returncode)\r | |
234 | \r | |
235 | #\r | |
236 | # Save output file contents from input file \r | |
237 | # \r | |
238 | open(args.OutputFileName, 'wb').write(args.InputFileBuffer)\r |