X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FRsa2048Sha256Sign%2FRsa2048Sha256Sign.py;h=2e164c4a2da6c319753a2396dc4962668f568f4f;hp=34106680eef07b8b637904a2cf0c01a41e4306eb;hb=92beb1e4c73a40a708c7c0cade5c7cee314b3887;hpb=9b98c4164013845ba80befd66fd38ce827a4c034 diff --git a/BaseTools/Source/Python/Rsa2048Sha256Sign/Rsa2048Sha256Sign.py b/BaseTools/Source/Python/Rsa2048Sha256Sign/Rsa2048Sha256Sign.py index 34106680ee..2e164c4a2d 100644 --- a/BaseTools/Source/Python/Rsa2048Sha256Sign/Rsa2048Sha256Sign.py +++ b/BaseTools/Source/Python/Rsa2048Sha256Sign/Rsa2048Sha256Sign.py @@ -4,7 +4,7 @@ # {0xa7717414, 0xc616, 0x4977, {0x94, 0x20, 0x84, 0x47, 0x12, 0xa7, 0x35, 0xbf}} # This tool has been tested with OpenSSL 1.0.1e 11 Feb 2013 # -# Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.
+# Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BSD License # which accompanies this distribution. The full text of the license may be found at @@ -17,6 +17,7 @@ ''' Rsa2048Sha256Sign ''' +from __future__ import print_function import os import sys @@ -85,6 +86,8 @@ if __name__ == '__main__': try: OpenSslPath = os.environ['OPENSSL_PATH'] OpenSslCommand = os.path.join(OpenSslPath, OpenSslCommand) + if ' ' in OpenSslCommand: + OpenSslCommand = '"' + OpenSslCommand + '"' except: pass @@ -92,16 +95,16 @@ if __name__ == '__main__': # Verify that Open SSL command is available # try: - Process = subprocess.Popen('%s version' % (OpenSslCommand), stdout=subprocess.PIPE, stderr=subprocess.PIPE) + Process = subprocess.Popen('%s version' % (OpenSslCommand), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) except: - print 'ERROR: Open SSL command not available. Please verify PATH or set OPENSSL_PATH' + print('ERROR: Open SSL command not available. Please verify PATH or set OPENSSL_PATH') sys.exit(1) Version = Process.communicate() - if Process.returncode <> 0: - print 'ERROR: Open SSL command not available. Please verify PATH or set OPENSSL_PATH' + if Process.returncode != 0: + print('ERROR: Open SSL command not available. Please verify PATH or set OPENSSL_PATH') sys.exit(Process.returncode) - print Version[0] + print(Version[0]) # # Read input file into a buffer and save input filename @@ -115,7 +118,7 @@ if __name__ == '__main__': # OutputDir = os.path.dirname(args.OutputFile) if not os.path.exists(OutputDir): - print 'ERROR: The output path does not exist: %s' % OutputDir + print('ERROR: The output path does not exist: %s' % OutputDir) sys.exit(1) args.OutputFileName = args.OutputFile @@ -142,19 +145,19 @@ if __name__ == '__main__': args.PrivateKeyFile = open(args.PrivateKeyFileName, 'rb') args.PrivateKeyFile.close() except: - print 'ERROR: test signing private key file %s missing' % (args.PrivateKeyFileName) + print('ERROR: test signing private key file %s missing' % (args.PrivateKeyFileName)) sys.exit(1) # # Extract public key from private key into STDOUT # - Process = subprocess.Popen('%s rsa -in "%s" -modulus -noout' % (OpenSslCommand, args.PrivateKeyFileName), stdout=subprocess.PIPE, stderr=subprocess.PIPE) + Process = subprocess.Popen('%s rsa -in "%s" -modulus -noout' % (OpenSslCommand, args.PrivateKeyFileName), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) PublicKeyHexString = Process.communicate()[0].split('=')[1].strip() PublicKey = '' while len(PublicKeyHexString) > 0: PublicKey = PublicKey + chr(int(PublicKeyHexString[0:2],16)) PublicKeyHexString=PublicKeyHexString[2:] - if Process.returncode <> 0: + if Process.returncode != 0: sys.exit(Process.returncode) if args.MonotonicCountStr: @@ -169,14 +172,14 @@ if __name__ == '__main__': if args.Encode: FullInputFileBuffer = args.InputFileBuffer if args.MonotonicCountStr: - format = "Q%ds" % len(args.InputFileBuffer) - FullInputFileBuffer = struct.pack(format,args.MonotonicCountValue, args.InputFileBuffer) + format = "%dsQ" % len(args.InputFileBuffer) + FullInputFileBuffer = struct.pack(format, args.InputFileBuffer, args.MonotonicCountValue) # # Sign the input file using the specified private key and capture signature from STDOUT # - Process = subprocess.Popen('%s sha256 -sign "%s"' % (OpenSslCommand, args.PrivateKeyFileName), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + Process = subprocess.Popen('%s dgst -sha256 -sign "%s"' % (OpenSslCommand, args.PrivateKeyFileName), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) Signature = Process.communicate(input=FullInputFileBuffer)[0] - if Process.returncode <> 0: + if Process.returncode != 0: sys.exit(Process.returncode) # @@ -199,21 +202,21 @@ if __name__ == '__main__': # # Verify that the Hash Type matches the expected SHA256 type # - if uuid.UUID(bytes_le = Header.HashType) <> EFI_HASH_ALGORITHM_SHA256_GUID: - print 'ERROR: unsupport hash GUID' + if uuid.UUID(bytes_le = Header.HashType) != EFI_HASH_ALGORITHM_SHA256_GUID: + print('ERROR: unsupport hash GUID') sys.exit(1) # # Verify the public key # - if Header.PublicKey <> PublicKey: - print 'ERROR: Public key in input file does not match public key from private key file' + if Header.PublicKey != PublicKey: + print('ERROR: Public key in input file does not match public key from private key file') sys.exit(1) FullInputFileBuffer = args.InputFileBuffer if args.MonotonicCountStr: - format = "Q%ds" % len(args.InputFileBuffer) - FullInputFileBuffer = struct.pack(format,args.MonotonicCountValue, args.InputFileBuffer) + format = "%dsQ" % len(args.InputFileBuffer) + FullInputFileBuffer = struct.pack(format, args.InputFileBuffer, args.MonotonicCountValue) # # Write Signature to output file @@ -223,10 +226,10 @@ if __name__ == '__main__': # # Verify signature # - Process = subprocess.Popen('%s sha256 -prverify "%s" -signature %s' % (OpenSslCommand, args.PrivateKeyFileName, args.OutputFileName), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + Process = subprocess.Popen('%s dgst -sha256 -prverify "%s" -signature %s' % (OpenSslCommand, args.PrivateKeyFileName, args.OutputFileName), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) Process.communicate(input=FullInputFileBuffer) - if Process.returncode <> 0: - print 'ERROR: Verification failed' + if Process.returncode != 0: + print('ERROR: Verification failed') os.remove (args.OutputFileName) sys.exit(Process.returncode)