## @file\r
-# This tool can be used to generate new RSA 2048 bit private/public key pairs \r
-# in a PEM file format using OpenSSL command line utilities that are installed \r
+# This tool can be used to generate new RSA 2048 bit private/public key pairs\r
+# in a PEM file format using OpenSSL command line utilities that are installed\r
# on the path specified by the system environment variable OPENSSL_PATH.\r
-# This tool can also optionally write one or more SHA 256 hashes of 2048 bit \r
-# public keys to a binary file, write one or more SHA 256 hashes of 2048 bit \r
-# public keys to a file in a C structure format, and in verbose mode display \r
-# one or more SHA 256 hashes of 2048 bit public keys in a C structure format \r
+# This tool can also optionally write one or more SHA 256 hashes of 2048 bit\r
+# public keys to a binary file, write one or more SHA 256 hashes of 2048 bit\r
+# public keys to a file in a C structure format, and in verbose mode display\r
+# one or more SHA 256 hashes of 2048 bit public keys in a C structure format\r
# on STDOUT.\r
# This tool has been tested with OpenSSL 1.0.1e 11 Feb 2013\r
#\r
# Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>\r
-# This program and the accompanying materials\r
-# are licensed and made available under the terms and conditions of the BSD License\r
-# which accompanies this distribution. The full text of the license may be found at\r
-# http://opensource.org/licenses/bsd-license.php\r
-#\r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
#\r
\r
'''\r
\r
import os\r
import sys\r
-import argparse \r
+import argparse\r
import subprocess\r
from Common.BuildVersion import gBUILD_VERSION\r
\r
#\r
__prog__ = 'Rsa2048Sha256GenerateKeys'\r
__version__ = '%s Version %s' % (__prog__, '0.9 ' + gBUILD_VERSION)\r
-__copyright__ = 'Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.'\r
+__copyright__ = 'Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.'\r
__usage__ = '%s [options]' % (__prog__)\r
\r
\r
if __name__ == '__main__':\r
#\r
# Create command line argument parser object\r
- # \r
- parser = argparse.ArgumentParser(prog=__prog__, version=__version__, usage=__usage__, description=__copyright__, conflict_handler='resolve')\r
+ #\r
+ parser = argparse.ArgumentParser(prog=__prog__, usage=__usage__, description=__copyright__, conflict_handler='resolve')\r
group = parser.add_mutually_exclusive_group(required=True)\r
+ group.add_argument("--version", action='version', version=__version__)\r
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
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
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
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
parser.add_argument("-v", "--verbose", dest='Verbose', action="store_true", help="increase output messages")\r
parser.add_argument("-q", "--quiet", dest='Quiet', action="store_true", help="reduce output messages")\r
- parser.add_argument("--debug", dest='Debug', type=int, metavar='[0-9]', choices=range(0,10), default=0, help="set debug level")\r
+ parser.add_argument("--debug", dest='Debug', type=int, metavar='[0-9]', choices=range(0, 10), default=0, help="set debug level")\r
\r
#\r
# Parse command line arguments\r
- # \r
+ #\r
args = parser.parse_args()\r
\r
#\r
#\r
try:\r
Process = subprocess.Popen('%s version' % (OpenSslCommand), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r
- except: \r
+ except:\r
print('ERROR: Open SSL command not available. Please verify PATH or set OPENSSL_PATH')\r
sys.exit(1)\r
- \r
+\r
Version = Process.communicate()\r
- if Process.returncode <> 0:\r
+ if Process.returncode != 0:\r
print('ERROR: Open SSL command not available. Please verify PATH or set OPENSSL_PATH')\r
sys.exit(Process.returncode)\r
- print(Version[0])\r
- \r
+ print(Version[0].decode(encoding='utf-8', errors='ignore'))\r
+\r
args.PemFileName = []\r
- \r
+\r
#\r
# Check for output file argument\r
#\r
#\r
Process = subprocess.Popen('%s genrsa -out %s 2048' % (OpenSslCommand, Item.name), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r
Process.communicate()\r
- if Process.returncode <> 0:\r
+ if Process.returncode != 0:\r
print('ERROR: RSA 2048 key generation failed')\r
sys.exit(Process.returncode)\r
- \r
+\r
#\r
# Check for input file argument\r
#\r
args.PemFileName.append(Item.name)\r
Item.close()\r
\r
- PublicKeyHash = ''\r
+ PublicKeyHash = bytearray()\r
for Item in args.PemFileName:\r
#\r
# Extract public key from private key into STDOUT\r
#\r
Process = subprocess.Popen('%s rsa -in %s -modulus -noout' % (OpenSslCommand, Item), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r
- PublicKeyHexString = Process.communicate()[0].split('=')[1].strip()\r
- if Process.returncode <> 0:\r
+ PublicKeyHexString = Process.communicate()[0].decode(encoding='utf-8', errors='ignore').split(b'=')[1].strip()\r
+ if Process.returncode != 0:\r
print('ERROR: Unable to extract public key from private key')\r
sys.exit(Process.returncode)\r
- PublicKey = ''\r
+ PublicKey = bytearray()\r
for Index in range (0, len(PublicKeyHexString), 2):\r
- PublicKey = PublicKey + chr(int(PublicKeyHexString[Index:Index + 2], 16))\r
+ PublicKey = PublicKey + PublicKeyHexString[Index:Index + 2]\r
\r
#\r
# Generate SHA 256 hash of RSA 2048 bit public key into STDOUT\r
#\r
Process = subprocess.Popen('%s dgst -sha256 -binary' % (OpenSslCommand), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r
Process.stdin.write (PublicKey)\r
- PublicKeyHash = PublicKeyHash + Process.communicate()[0]\r
- if Process.returncode <> 0:\r
+ PublicKeyHash = PublicKeyHash + Process.communicate()[0].decode(encoding='utf-8', errors='ignore')\r
+ if Process.returncode != 0:\r
print('ERROR: Unable to extract SHA 256 hash of public key')\r
sys.exit(Process.returncode)\r
\r
#\r
PublicKeyHashC = '{'\r
for Item in PublicKeyHash:\r
- PublicKeyHashC = PublicKeyHashC + '0x%02x, ' % (ord(Item))\r
+ PublicKeyHashC = PublicKeyHashC + '0x%02x, ' % (Item)\r
PublicKeyHashC = PublicKeyHashC[:-2] + '}'\r
- \r
+\r
#\r
# Write SHA 256 of 2048 bit binary public key to public key hash C structure file\r
#\r
try:\r
- args.PublicKeyHashCFile.write (PublicKeyHashC)\r
+ args.PublicKeyHashCFile.write (bytes(PublicKeyHashC))\r
args.PublicKeyHashCFile.close ()\r
except:\r
pass\r
- \r
+\r
#\r
# If verbose is enabled display the public key in C structure format\r
#\r