]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Rsa2048Sha256Sign/Rsa2048Sha256GenerateKeys.py
BaseTools: Update Pkcs7 and RSA2048 tool with shell=True
[mirror_edk2.git] / BaseTools / Source / Python / Rsa2048Sha256Sign / Rsa2048Sha256GenerateKeys.py
CommitLineData
65ce860e
MK
1## @file\r
2# This tool can be used to generate new RSA 2048 bit private/public key pairs \r
3# in a PEM file format using OpenSSL command line utilities that are installed \r
4# on the path specified by the system environment variable OPENSSL_PATH.\r
5# This tool can also optionally write one or more SHA 256 hashes of 2048 bit \r
6# public keys to a binary file, write one or more SHA 256 hashes of 2048 bit \r
7# public keys to a file in a C structure format, and in verbose mode display \r
8# one or more SHA 256 hashes of 2048 bit public keys in a C structure format \r
9# on STDOUT.\r
10# This tool has been tested with OpenSSL 1.0.1e 11 Feb 2013\r
11#\r
8a0933f4 12# Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>\r
65ce860e
MK
13# This program and the accompanying materials\r
14# are licensed and made available under the terms and conditions of the BSD License\r
15# which accompanies this distribution. The full text of the license may be found at\r
16# http://opensource.org/licenses/bsd-license.php\r
17#\r
18# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
19# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
20#\r
21\r
22'''\r
23Rsa2048Sha256GenerateKeys\r
24'''\r
25\r
26import os\r
27import sys\r
28import argparse \r
29import subprocess\r
c9df168f
MK
30from Common.BuildVersion import gBUILD_VERSION\r
31\r
32#\r
33# Globals for help information\r
34#\r
35__prog__ = 'Rsa2048Sha256GenerateKeys'\r
36__version__ = '%s Version %s' % (__prog__, '0.9 ' + gBUILD_VERSION)\r
37__copyright__ = 'Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.'\r
38__usage__ = '%s [options]' % (__prog__)\r
39\r
65ce860e
MK
40\r
41if __name__ == '__main__':\r
65ce860e
MK
42 #\r
43 # Create command line argument parser object\r
44 # \r
c9df168f 45 parser = argparse.ArgumentParser(prog=__prog__, version=__version__, usage=__usage__, description=__copyright__, conflict_handler='resolve')\r
65ce860e
MK
46 group = parser.add_mutually_exclusive_group(required=True)\r
47 group.add_argument("-o", "--output", dest='OutputFile', type=argparse.FileType('wb'), metavar='filename', nargs='*', help="specify the output private key filename in PEM format")\r
48 group.add_argument("-i", "--input", dest='InputFile', type=argparse.FileType('rb'), metavar='filename', nargs='*', help="specify the input private key filename in PEM format")\r
49 parser.add_argument("--public-key-hash", dest='PublicKeyHashFile', type=argparse.FileType('wb'), help="specify the public key hash filename that is SHA 256 hash of 2048 bit RSA public key in binary format")\r
50 parser.add_argument("--public-key-hash-c", dest='PublicKeyHashCFile', type=argparse.FileType('wb'), help="specify the public key hash filename that is SHA 256 hash of 2048 bit RSA public key in C structure format")\r
51 parser.add_argument("-v", "--verbose", dest='Verbose', action="store_true", help="increase output messages")\r
52 parser.add_argument("-q", "--quiet", dest='Quiet', action="store_true", help="reduce output messages")\r
53 parser.add_argument("--debug", dest='Debug', type=int, metavar='[0-9]', choices=range(0,10), default=0, help="set debug level")\r
65ce860e
MK
54\r
55 #\r
56 # Parse command line arguments\r
57 # \r
58 args = parser.parse_args()\r
59\r
60 #\r
61 # Generate file path to Open SSL command\r
62 #\r
63 OpenSslCommand = 'openssl'\r
64 try:\r
65 OpenSslPath = os.environ['OPENSSL_PATH']\r
66 OpenSslCommand = os.path.join(OpenSslPath, OpenSslCommand)\r
67 except:\r
68 pass\r
69\r
70 #\r
71 # Verify that Open SSL command is available\r
72 #\r
73 try:\r
8a0933f4 74 Process = subprocess.Popen('%s version' % (OpenSslCommand), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r
65ce860e
MK
75 except: \r
76 print 'ERROR: Open SSL command not available. Please verify PATH or set OPENSSL_PATH'\r
77 sys.exit(1)\r
78 \r
79 Version = Process.communicate()\r
80 if Process.returncode <> 0:\r
81 print 'ERROR: Open SSL command not available. Please verify PATH or set OPENSSL_PATH'\r
82 sys.exit(Process.returncode)\r
83 print Version[0]\r
84 \r
85 args.PemFileName = []\r
86 \r
87 #\r
88 # Check for output file argument\r
89 #\r
90 if args.OutputFile <> None:\r
91 for Item in args.OutputFile:\r
92 #\r
93 # Save PEM filename and close output file\r
94 #\r
95 args.PemFileName.append(Item.name)\r
96 Item.close()\r
97\r
98 #\r
99 # Generate private key and save it to output file in a PEM file format\r
100 #\r
a5f26fef 101 Process = subprocess.Popen('%s genrsa -out %s 2048' % (OpenSslCommand, Item.name), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r
65ce860e
MK
102 Process.communicate()\r
103 if Process.returncode <> 0:\r
104 print 'ERROR: RSA 2048 key generation failed'\r
105 sys.exit(Process.returncode)\r
106 \r
107 #\r
108 # Check for input file argument\r
109 #\r
110 if args.InputFile <> None:\r
111 for Item in args.InputFile:\r
112 #\r
113 # Save PEM filename and close input file\r
114 #\r
115 args.PemFileName.append(Item.name)\r
116 Item.close()\r
117\r
118 PublicKeyHash = ''\r
119 for Item in args.PemFileName:\r
120 #\r
121 # Extract public key from private key into STDOUT\r
122 #\r
a5f26fef 123 Process = subprocess.Popen('%s rsa -in %s -modulus -noout' % (OpenSslCommand, Item), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r
65ce860e
MK
124 PublicKeyHexString = Process.communicate()[0].split('=')[1].strip()\r
125 if Process.returncode <> 0:\r
126 print 'ERROR: Unable to extract public key from private key'\r
127 sys.exit(Process.returncode)\r
128 PublicKey = ''\r
129 for Index in range (0, len(PublicKeyHexString), 2):\r
130 PublicKey = PublicKey + chr(int(PublicKeyHexString[Index:Index + 2], 16))\r
131\r
132 #\r
133 # Generate SHA 256 hash of RSA 2048 bit public key into STDOUT\r
134 #\r
a5f26fef 135 Process = subprocess.Popen('%s dgst -sha256 -binary' % (OpenSslCommand), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r
65ce860e
MK
136 Process.stdin.write (PublicKey)\r
137 PublicKeyHash = PublicKeyHash + Process.communicate()[0]\r
138 if Process.returncode <> 0:\r
139 print 'ERROR: Unable to extract SHA 256 hash of public key'\r
140 sys.exit(Process.returncode)\r
141\r
142 #\r
143 # Write SHA 256 hash of 2048 bit binary public key to public key hash file\r
144 #\r
145 try:\r
146 args.PublicKeyHashFile.write (PublicKeyHash)\r
147 args.PublicKeyHashFile.close ()\r
148 except:\r
149 pass\r
150\r
151 #\r
152 # Convert public key hash to a C structure string\r
153 #\r
154 PublicKeyHashC = '{'\r
155 for Item in PublicKeyHash:\r
156 PublicKeyHashC = PublicKeyHashC + '0x%02x, ' % (ord(Item))\r
157 PublicKeyHashC = PublicKeyHashC[:-2] + '}'\r
158 \r
159 #\r
160 # Write SHA 256 of 2048 bit binary public key to public key hash C structure file\r
161 #\r
162 try:\r
163 args.PublicKeyHashCFile.write (PublicKeyHashC)\r
164 args.PublicKeyHashCFile.close ()\r
165 except:\r
166 pass\r
167 \r
168 #\r
169 # If verbose is enabled display the public key in C structure format\r
170 #\r
171 if args.Verbose:\r
172 print 'PublicKeySha256 = ' + PublicKeyHashC \r