]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Rsa2048Sha256Sign/Rsa2048Sha256GenerateKeys.py
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / BaseTools / Source / Python / Rsa2048Sha256Sign / Rsa2048Sha256GenerateKeys.py
index a34dac423b8c1eeb5698917e3e0b9422c12ea9dc..6c9b8c464e4db5643355a9c2e74ed4698750c7b3 100644 (file)
 # 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
@@ -43,8 +37,9 @@ if __name__ == '__main__':
   #\r
   # Create command line argument parser object\r
   #\r
-  parser = argparse.ArgumentParser(prog=__prog__, version=__version__, usage=__usage__, description=__copyright__, conflict_handler='resolve')\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
@@ -83,7 +78,7 @@ if __name__ == '__main__':
   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
+  print(Version[0].decode())\r
 \r
   args.PemFileName = []\r
 \r
@@ -118,26 +113,26 @@ if __name__ == '__main__':
       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
+    PublicKeyHexString = Process.communicate()[0].decode().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
+    PublicKeyHash = PublicKeyHash + Process.communicate()[0].decode()\r
     if Process.returncode != 0:\r
       print('ERROR: Unable to extract SHA 256 hash of public key')\r
       sys.exit(Process.returncode)\r
@@ -156,14 +151,14 @@ if __name__ == '__main__':
   #\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
   # 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