Commit | Line | Data |
---|---|---|
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 | |
128d435f | 12 | # Copyright (c) 2013 - 2018, 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 | |
23 | Rsa2048Sha256GenerateKeys\r | |
24 | '''\r | |
72443dd2 | 25 | from __future__ import print_function\r |
65ce860e MK |
26 | \r |
27 | import os\r | |
28 | import sys\r | |
29 | import argparse \r | |
30 | import subprocess\r | |
c9df168f MK |
31 | from Common.BuildVersion import gBUILD_VERSION\r |
32 | \r | |
33 | #\r | |
34 | # Globals for help information\r | |
35 | #\r | |
36 | __prog__ = 'Rsa2048Sha256GenerateKeys'\r | |
37 | __version__ = '%s Version %s' % (__prog__, '0.9 ' + gBUILD_VERSION)\r | |
38 | __copyright__ = 'Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.'\r | |
39 | __usage__ = '%s [options]' % (__prog__)\r | |
40 | \r | |
65ce860e MK |
41 | \r |
42 | if __name__ == '__main__':\r | |
65ce860e MK |
43 | #\r |
44 | # Create command line argument parser object\r | |
45 | # \r | |
c9df168f | 46 | parser = argparse.ArgumentParser(prog=__prog__, version=__version__, usage=__usage__, description=__copyright__, conflict_handler='resolve')\r |
65ce860e MK |
47 | group = parser.add_mutually_exclusive_group(required=True)\r |
48 | 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 | |
49 | 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 | |
50 | 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 | |
51 | 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 | |
52 | parser.add_argument("-v", "--verbose", dest='Verbose', action="store_true", help="increase output messages")\r | |
53 | parser.add_argument("-q", "--quiet", dest='Quiet', action="store_true", help="reduce output messages")\r | |
54 | parser.add_argument("--debug", dest='Debug', type=int, metavar='[0-9]', choices=range(0,10), default=0, help="set debug level")\r | |
65ce860e MK |
55 | \r |
56 | #\r | |
57 | # Parse command line arguments\r | |
58 | # \r | |
59 | args = parser.parse_args()\r | |
60 | \r | |
61 | #\r | |
62 | # Generate file path to Open SSL command\r | |
63 | #\r | |
64 | OpenSslCommand = 'openssl'\r | |
65 | try:\r | |
66 | OpenSslPath = os.environ['OPENSSL_PATH']\r | |
67 | OpenSslCommand = os.path.join(OpenSslPath, OpenSslCommand)\r | |
fcdb928a YZ |
68 | if ' ' in OpenSslCommand:\r |
69 | OpenSslCommand = '"' + OpenSslCommand + '"'\r | |
65ce860e MK |
70 | except:\r |
71 | pass\r | |
72 | \r | |
73 | #\r | |
74 | # Verify that Open SSL command is available\r | |
75 | #\r | |
76 | try:\r | |
8a0933f4 | 77 | Process = subprocess.Popen('%s version' % (OpenSslCommand), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r |
65ce860e | 78 | except: \r |
72443dd2 | 79 | print('ERROR: Open SSL command not available. Please verify PATH or set OPENSSL_PATH')\r |
65ce860e MK |
80 | sys.exit(1)\r |
81 | \r | |
82 | Version = Process.communicate()\r | |
87d2afd0 | 83 | if Process.returncode != 0:\r |
72443dd2 | 84 | print('ERROR: Open SSL command not available. Please verify PATH or set OPENSSL_PATH')\r |
65ce860e | 85 | sys.exit(Process.returncode)\r |
72443dd2 | 86 | print(Version[0])\r |
65ce860e MK |
87 | \r |
88 | args.PemFileName = []\r | |
89 | \r | |
90 | #\r | |
91 | # Check for output file argument\r | |
92 | #\r | |
128d435f | 93 | if args.OutputFile is not None:\r |
65ce860e MK |
94 | for Item in args.OutputFile:\r |
95 | #\r | |
96 | # Save PEM filename and close output file\r | |
97 | #\r | |
98 | args.PemFileName.append(Item.name)\r | |
99 | Item.close()\r | |
100 | \r | |
101 | #\r | |
102 | # Generate private key and save it to output file in a PEM file format\r | |
103 | #\r | |
a5f26fef | 104 | Process = subprocess.Popen('%s genrsa -out %s 2048' % (OpenSslCommand, Item.name), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r |
65ce860e | 105 | Process.communicate()\r |
87d2afd0 | 106 | if Process.returncode != 0:\r |
72443dd2 | 107 | print('ERROR: RSA 2048 key generation failed')\r |
65ce860e MK |
108 | sys.exit(Process.returncode)\r |
109 | \r | |
110 | #\r | |
111 | # Check for input file argument\r | |
112 | #\r | |
128d435f | 113 | if args.InputFile is not None:\r |
65ce860e MK |
114 | for Item in args.InputFile:\r |
115 | #\r | |
116 | # Save PEM filename and close input file\r | |
117 | #\r | |
118 | args.PemFileName.append(Item.name)\r | |
119 | Item.close()\r | |
120 | \r | |
121 | PublicKeyHash = ''\r | |
122 | for Item in args.PemFileName:\r | |
123 | #\r | |
124 | # Extract public key from private key into STDOUT\r | |
125 | #\r | |
a5f26fef | 126 | Process = subprocess.Popen('%s rsa -in %s -modulus -noout' % (OpenSslCommand, Item), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r |
65ce860e | 127 | PublicKeyHexString = Process.communicate()[0].split('=')[1].strip()\r |
87d2afd0 | 128 | if Process.returncode != 0:\r |
72443dd2 | 129 | print('ERROR: Unable to extract public key from private key')\r |
65ce860e MK |
130 | sys.exit(Process.returncode)\r |
131 | PublicKey = ''\r | |
132 | for Index in range (0, len(PublicKeyHexString), 2):\r | |
133 | PublicKey = PublicKey + chr(int(PublicKeyHexString[Index:Index + 2], 16))\r | |
134 | \r | |
135 | #\r | |
136 | # Generate SHA 256 hash of RSA 2048 bit public key into STDOUT\r | |
137 | #\r | |
a5f26fef | 138 | Process = subprocess.Popen('%s dgst -sha256 -binary' % (OpenSslCommand), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r |
65ce860e MK |
139 | Process.stdin.write (PublicKey)\r |
140 | PublicKeyHash = PublicKeyHash + Process.communicate()[0]\r | |
87d2afd0 | 141 | if Process.returncode != 0:\r |
72443dd2 | 142 | print('ERROR: Unable to extract SHA 256 hash of public key')\r |
65ce860e MK |
143 | sys.exit(Process.returncode)\r |
144 | \r | |
145 | #\r | |
146 | # Write SHA 256 hash of 2048 bit binary public key to public key hash file\r | |
147 | #\r | |
148 | try:\r | |
149 | args.PublicKeyHashFile.write (PublicKeyHash)\r | |
150 | args.PublicKeyHashFile.close ()\r | |
151 | except:\r | |
152 | pass\r | |
153 | \r | |
154 | #\r | |
155 | # Convert public key hash to a C structure string\r | |
156 | #\r | |
157 | PublicKeyHashC = '{'\r | |
158 | for Item in PublicKeyHash:\r | |
159 | PublicKeyHashC = PublicKeyHashC + '0x%02x, ' % (ord(Item))\r | |
160 | PublicKeyHashC = PublicKeyHashC[:-2] + '}'\r | |
161 | \r | |
162 | #\r | |
163 | # Write SHA 256 of 2048 bit binary public key to public key hash C structure file\r | |
164 | #\r | |
165 | try:\r | |
166 | args.PublicKeyHashCFile.write (PublicKeyHashC)\r | |
167 | args.PublicKeyHashCFile.close ()\r | |
168 | except:\r | |
169 | pass\r | |
170 | \r | |
171 | #\r | |
172 | # If verbose is enabled display the public key in C structure format\r | |
173 | #\r | |
174 | if args.Verbose:\r | |
72443dd2 | 175 | print('PublicKeySha256 = ' + PublicKeyHashC)\r |