From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 1BED61A1E74 for ; Thu, 1 Sep 2016 17:21:34 -0700 (PDT) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP; 01 Sep 2016 17:21:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.30,269,1470726000"; d="scan'208";a="1023952308" Received: from fmsmsx104.amr.corp.intel.com ([10.18.124.202]) by orsmga001.jf.intel.com with ESMTP; 01 Sep 2016 17:21:33 -0700 Received: from fmsmsx153.amr.corp.intel.com (10.18.125.6) by fmsmsx104.amr.corp.intel.com (10.18.124.202) with Microsoft SMTP Server (TLS) id 14.3.248.2; Thu, 1 Sep 2016 17:17:52 -0700 Received: from shsmsx151.ccr.corp.intel.com (10.239.6.50) by FMSMSX153.amr.corp.intel.com (10.18.125.6) with Microsoft SMTP Server (TLS) id 14.3.248.2; Thu, 1 Sep 2016 17:17:52 -0700 Received: from shsmsx102.ccr.corp.intel.com ([169.254.2.109]) by SHSMSX151.ccr.corp.intel.com ([169.254.3.250]) with mapi id 14.03.0248.002; Fri, 2 Sep 2016 08:17:49 +0800 From: "Gao, Liming" To: "Zhu, Yonghong" , "edk2-devel@lists.01.org" Thread-Topic: [edk2] [Patch] BaseTools: Change source files to DOS format Thread-Index: AQHSBDFa0MAZschEDUSoN5b6Yezmv6BlVnbQ Date: Fri, 2 Sep 2016 00:17:49 +0000 Message-ID: <4A89E2EF3DFEDB4C8BFDE51014F606A14B3E155E@shsmsx102.ccr.corp.intel.com> References: <1472721293-84472-1-git-send-email-yonghong.zhu@intel.com> In-Reply-To: <1472721293-84472-1-git-send-email-yonghong.zhu@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ctpclassification: CTP_IC x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiNDY3MzUzNDEtNjY2Zi00MGEwLTk2NGUtNDZlN2YwM2Q5YjZkIiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX0lDIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE1LjkuNi42IiwiVHJ1c3RlZExhYmVsSGFzaCI6ImlCM1UwTDFBc0lHd05tVXZwNU4yc1ZYcVVuTHNPUkRqUVdWV1dhazg0UEk9In0= x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [Patch] BaseTools: Change source files to DOS format X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 02 Sep 2016 00:21:34 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Liming Gao -----Original Message----- From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Yong= hong Zhu Sent: Thursday, September 1, 2016 5:15 PM To: edk2-devel@lists.01.org Cc: Gao, Liming Subject: [edk2] [Patch] BaseTools: Change source files to DOS format Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Yonghong Zhu --- BaseTools/Source/C/GenFv/GenFvInternalLib.c | 12 +- BaseTools/Source/Python/Pkcs7Sign/Pkcs7Sign.py | 564 ++++++++++++---------= ---- 2 files changed, 288 insertions(+), 288 deletions(-) diff --git a/BaseTools/Source/C/GenFv/GenFvInternalLib.c b/BaseTools/Source= /C/GenFv/GenFvInternalLib.c index 123e355..fab7c94 100644 --- a/BaseTools/Source/C/GenFv/GenFvInternalLib.c +++ b/BaseTools/Source/C/GenFv/GenFvInternalLib.c @@ -2768,17 +2768,17 @@ Returns: } if (!mArm) { // // Update reset vector (SALE_ENTRY for IPF) // Now for IA32 and IA64 platform, the fv which has bsf file must ha= ve the=20 - // EndAddress of 0xFFFFFFFF (unless the section was rebased). - // Thus, only this type fv needs to update the reset vector. - // If the PEI Core is found, the VTF file will probably get - // corrupted by updating the entry point. + // EndAddress of 0xFFFFFFFF (unless the section was rebased). + // Thus, only this type fv needs to update the reset vector. + // If the PEI Core is found, the VTF file will probably get + // corrupted by updating the entry point. // - if (mFvDataInfo.ForceRebase =3D=3D 1 || - (mFvDataInfo.BaseAddress + mFvDataInfo.Size) =3D=3D FV_IMAGES_TO= P_ADDRESS) { + if (mFvDataInfo.ForceRebase =3D=3D 1 || + (mFvDataInfo.BaseAddress + mFvDataInfo.Size) =3D=3D FV_IMAGES_TO= P_ADDRESS) { Status =3D UpdateResetVector (&FvImageMemoryFile, &mFvDataInfo, Vt= fFileImage); if (EFI_ERROR(Status)) { = =20 Error (NULL, 0, 3000, "Invalid", "Could not update the reset vec= tor."); goto Finish; =20 } diff --git a/BaseTools/Source/Python/Pkcs7Sign/Pkcs7Sign.py b/BaseTools/Sou= rce/Python/Pkcs7Sign/Pkcs7Sign.py index 1998d6c..c1977d6 100644 --- a/BaseTools/Source/Python/Pkcs7Sign/Pkcs7Sign.py +++ b/BaseTools/Source/Python/Pkcs7Sign/Pkcs7Sign.py @@ -1,282 +1,282 @@ -## @file -# This tool adds EFI_FIRMWARE_IMAGE_AUTHENTICATION for a binary. -# -# This tool only support CertType - EFI_CERT_TYPE_PKCS7_GUID -# {0x4aafd29d, 0x68df, 0x49ee, {0x8a, 0xa9, 0x34, 0x7d, 0x37, 0x56, 0x65= , 0xa7}} -# -# This tool has been tested with OpenSSL. -# -# Copyright (c) 2016, Intel Corporation. All rights reserved.
-# This program and the accompanying materials -# are licensed and made available under the terms and conditions of the BS= D License -# which accompanies this distribution. The full text of the license may b= e found at -# http://opensource.org/licenses/bsd-license.php -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMP= LIED. -# - -''' -Pkcs7Sign -''' - -import os -import sys -import argparse=20 -import subprocess -import uuid -import struct -import collections -from Common.BuildVersion import gBUILD_VERSION - -# -# Globals for help information -# -__prog__ =3D 'Pkcs7Sign' -__version__ =3D '%s Version %s' % (__prog__, '0.9 ' + gBUILD_VERSION) -__copyright__ =3D 'Copyright (c) 2016, Intel Corporation. All rights reser= ved.' -__usage__ =3D '%s -e|-d [options] ' % (__prog__) - -# -# GUID for PKCS7 from UEFI Specification -# -WIN_CERT_REVISION =3D 0x0200 -WIN_CERT_TYPE_EFI_GUID =3D 0x0EF1 -EFI_CERT_TYPE_PKCS7_GUID =3D uuid.UUID('{4aafd29d-68df-49ee-8aa9-347d37566= 5a7}') - -# -# typedef struct _WIN_CERTIFICATE { -# UINT32 dwLength; -# UINT16 wRevision; -# UINT16 wCertificateType; -# //UINT8 bCertificate[ANYSIZE_ARRAY]; -# } WIN_CERTIFICATE; -# -# typedef struct _WIN_CERTIFICATE_UEFI_GUID { -# WIN_CERTIFICATE Hdr; -# EFI_GUID CertType; -# //UINT8 CertData[ANYSIZE_ARRAY]; -# } WIN_CERTIFICATE_UEFI_GUID; -# -# typedef struct { -# UINT64 MonotonicCount; -# WIN_CERTIFICATE_UEFI_GUID AuthInfo; -# } EFI_FIRMWARE_IMAGE_AUTHENTICATION; -# - -# -# Filename of test signing private cert that is stored in same directory a= s this tool -# -TEST_SIGNER_PRIVATE_CERT_FILENAME =3D 'TestCert.pem' -TEST_OTHER_PUBLIC_CERT_FILENAME =3D 'TestSub.pub.pem' -TEST_TRUSTED_PUBLIC_CERT_FILENAME =3D 'TestRoot.pub.pem' - -if __name__ =3D=3D '__main__': - # - # Create command line argument parser object - # =20 - parser =3D argparse.ArgumentParser(prog=3D__prog__, version=3D__version_= _, usage=3D__usage__, description=3D__copyright__, conflict_handler=3D'reso= lve') - group =3D parser.add_mutually_exclusive_group(required=3DTrue) - group.add_argument("-e", action=3D"store_true", dest=3D'Encode', help=3D= 'encode file') - group.add_argument("-d", action=3D"store_true", dest=3D'Decode', help=3D= 'decode file') - parser.add_argument("-o", "--output", dest=3D'OutputFile', type=3Dstr, m= etavar=3D'filename', help=3D"specify the output filename", required=3DTrue) - parser.add_argument("--signer-private-cert", dest=3D'SignerPrivateCertFi= le', type=3Dargparse.FileType('rb'), help=3D"specify the signer private cer= t filename. If not specified, a test signer private cert is used.") - parser.add_argument("--other-public-cert", dest=3D'OtherPublicCertFile',= type=3Dargparse.FileType('rb'), help=3D"specify the other public cert file= name. If not specified, a test other public cert is used.") - parser.add_argument("--trusted-public-cert", dest=3D'TrustedPublicCertFi= le', type=3Dargparse.FileType('rb'), help=3D"specify the trusted public cer= t filename. If not specified, a test trusted public cert is used.") - parser.add_argument("--monotonic-count", dest=3D'MonotonicCountStr', typ= e=3Dstr, help=3D"specify the MonotonicCount in FMP capsule. If not specifi= ed, 0 is used.") - parser.add_argument("--signature-size", dest=3D'SignatureSizeStr', type= =3Dstr, help=3D"specify the signature size for decode process.") - parser.add_argument("-v", "--verbose", dest=3D'Verbose', action=3D"store= _true", help=3D"increase output messages") - parser.add_argument("-q", "--quiet", dest=3D'Quiet', action=3D"store_tru= e", help=3D"reduce output messages") - parser.add_argument("--debug", dest=3D'Debug', type=3Dint, metavar=3D'[0= -9]', choices=3Drange(0,10), default=3D0, help=3D"set debug level") - parser.add_argument(metavar=3D"input_file", dest=3D'InputFile', type=3Da= rgparse.FileType('rb'), help=3D"specify the input filename") - - # - # Parse command line arguments - # =20 - args =3D parser.parse_args() - - # - # Generate file path to Open SSL command - # - OpenSslCommand =3D 'openssl' - try: - OpenSslPath =3D os.environ['OPENSSL_PATH'] - OpenSslCommand =3D os.path.join(OpenSslPath, OpenSslCommand) - except: - pass - - # - # Verify that Open SSL command is available - # - try: - Process =3D subprocess.Popen('%s version' % (OpenSslCommand), stdout= =3Dsubprocess.PIPE, stderr=3Dsubprocess.PIPE) - except: =20 - print 'ERROR: Open SSL command not available. Please verify PATH or s= et OPENSSL_PATH' - sys.exit(1) - - Version =3D Process.communicate() - if Process.returncode <> 0: - print 'ERROR: Open SSL command not available. Please verify PATH or s= et OPENSSL_PATH' - sys.exit(Process.returncode) - print Version[0] - - # - # Read input file into a buffer and save input filename - # =20 - args.InputFileName =3D args.InputFile.name - args.InputFileBuffer =3D args.InputFile.read() - args.InputFile.close() - - # - # Save output filename and check if path exists - # - OutputDir =3D os.path.dirname(args.OutputFile) - if not os.path.exists(OutputDir): - print 'ERROR: The output path does not exist: %s' % OutputDir - sys.exit(1) - args.OutputFileName =3D args.OutputFile - - try: - if args.MonotonicCountStr.upper().startswith('0X'): - args.MonotonicCountValue =3D (long)(args.MonotonicCountStr, 16) - else: - args.MonotonicCountValue =3D (long)(args.MonotonicCountStr) - except: - args.MonotonicCountValue =3D (long)(0) - - if args.Encode: - # - # Save signer private cert filename and close private cert file - # - try: - args.SignerPrivateCertFileName =3D args.SignerPrivateCertFile.name - args.SignerPrivateCertFile.close() - except: - try: - # - # Get path to currently executing script or executable - # - if hasattr(sys, 'frozen'): - Pkcs7ToolPath =3D sys.executable - else: - Pkcs7ToolPath =3D sys.argv[0] - if Pkcs7ToolPath.startswith('"'): - Pkcs7ToolPath =3D Pkcs7ToolPath[1:] - if Pkcs7ToolPath.endswith('"'): - Pkcs7ToolPath =3D RsaToolPath[:-1] - args.SignerPrivateCertFileName =3D os.path.join(os.path.dirname(os= .path.realpath(Pkcs7ToolPath)), TEST_SIGNER_PRIVATE_CERT_FILENAME) - args.SignerPrivateCertFile =3D open(args.SignerPrivateCertFileName= , 'rb') - args.SignerPrivateCertFile.close() - except: - print 'ERROR: test signer private cert file %s missing' % (args.Si= gnerPrivateCertFileName) - sys.exit(1) - - # - # Save other public cert filename and close public cert file - # - try: - args.OtherPublicCertFileName =3D args.OtherPublicCertFile.name - args.OtherPublicCertFile.close() - except: - try: - # - # Get path to currently executing script or executable - # - if hasattr(sys, 'frozen'): - Pkcs7ToolPath =3D sys.executable - else: - Pkcs7ToolPath =3D sys.argv[0] - if Pkcs7ToolPath.startswith('"'): - Pkcs7ToolPath =3D Pkcs7ToolPath[1:] - if Pkcs7ToolPath.endswith('"'): - Pkcs7ToolPath =3D RsaToolPath[:-1] - args.OtherPublicCertFileName =3D os.path.join(os.path.dirname(os.p= ath.realpath(Pkcs7ToolPath)), TEST_OTHER_PUBLIC_CERT_FILENAME) - args.OtherPublicCertFile =3D open(args.OtherPublicCertFileName, 'r= b') - args.OtherPublicCertFile.close() - except: - print 'ERROR: test other public cert file %s missing' % (args.Othe= rPublicCertFileName) - sys.exit(1) - - format =3D "Q%ds" % len(args.InputFileBuffer) - FullInputFileBuffer =3D struct.pack(format,args.MonotonicCountValue, a= rgs.InputFileBuffer) - - #=20 - # Sign the input file using the specified private key and capture sign= ature from STDOUT - # - Process =3D subprocess.Popen('%s smime -sign -binary -signer "%s" -out= form DER -md sha256 -certfile "%s"' % (OpenSslCommand, args.SignerPrivateCe= rtFileName, args.OtherPublicCertFileName), stdin=3Dsubprocess.PIPE, stdout= =3Dsubprocess.PIPE, stderr=3Dsubprocess.PIPE) - Signature =3D Process.communicate(input=3DFullInputFileBuffer)[0] - if Process.returncode <> 0: - sys.exit(Process.returncode) - - # - # Write output file that contains Signature, and Input data - # =20 - args.OutputFile =3D open(args.OutputFileName, 'wb') - args.OutputFile.write(Signature) - args.OutputFile.write(args.InputFileBuffer) - args.OutputFile.close() - - if args.Decode: - # - # Save trusted public cert filename and close public cert file - # - try: - args.TrustedPublicCertFileName =3D args.TrustedPublicCertFile.name - args.TrustedPublicCertFile.close() - except: - try: - # - # Get path to currently executing script or executable - # - if hasattr(sys, 'frozen'): - Pkcs7ToolPath =3D sys.executable - else: - Pkcs7ToolPath =3D sys.argv[0] - if Pkcs7ToolPath.startswith('"'): - Pkcs7ToolPath =3D Pkcs7ToolPath[1:] - if Pkcs7ToolPath.endswith('"'): - Pkcs7ToolPath =3D RsaToolPath[:-1] - args.TrustedPublicCertFileName =3D os.path.join(os.path.dirname(os= .path.realpath(Pkcs7ToolPath)), TEST_TRUSTED_PUBLIC_CERT_FILENAME) - args.TrustedPublicCertFile =3D open(args.TrustedPublicCertFileName= , 'rb') - args.TrustedPublicCertFile.close() - except: - print 'ERROR: test trusted public cert file %s missing' % (args.Tr= ustedPublicCertFileName) - sys.exit(1) - - if not args.SignatureSizeStr: - print "ERROR: please use the option --signature-size to specify the = size of the signature data!" - sys.exit(1) - else: - if args.SignatureSizeStr.upper().startswith('0X'): - SignatureSize =3D (long)(args.SignatureSizeStr, 16) - else: - SignatureSize =3D (long)(args.SignatureSizeStr) - if SignatureSize < 0: - print "ERROR: The value of option --signature-size can't be set to= negative value!" - sys.exit(1) - elif SignatureSize > len(args.InputFileBuffer): - print "ERROR: The value of option --signature-size is exceed the s= ize of the input file !" - sys.exit(1) - - args.SignatureBuffer =3D args.InputFileBuffer[0:SignatureSize] - args.InputFileBuffer =3D args.InputFileBuffer[SignatureSize:] - - format =3D "Q%ds" % len(args.InputFileBuffer) - FullInputFileBuffer =3D struct.pack(format,args.MonotonicCountValue, a= rgs.InputFileBuffer) - - # - # Save output file contents from input file=20 - # - open(args.OutputFileName, 'wb').write(FullInputFileBuffer) - - # - # Verify signature - # - Process =3D subprocess.Popen('%s smime -verify -inform DER -content %s= -CAfile %s' % (OpenSslCommand, args.OutputFileName, args.TrustedPublicCert= FileName), stdin=3Dsubprocess.PIPE, stdout=3Dsubprocess.PIPE, stderr=3Dsubp= rocess.PIPE) - Process.communicate(input=3Dargs.SignatureBuffer)[0] - if Process.returncode <> 0: - print 'ERROR: Verification failed' - os.remove (args.OutputFileName) - sys.exit(Process.returncode) - - open(args.OutputFileName, 'wb').write(args.InputFileBuffer) +## @file +# This tool adds EFI_FIRMWARE_IMAGE_AUTHENTICATION for a binary. +# +# This tool only support CertType - EFI_CERT_TYPE_PKCS7_GUID +# {0x4aafd29d, 0x68df, 0x49ee, {0x8a, 0xa9, 0x34, 0x7d, 0x37, 0x56, 0x65= , 0xa7}} +# +# This tool has been tested with OpenSSL. +# +# Copyright (c) 2016, Intel Corporation. All rights reserved.
+# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the BS= D License +# which accompanies this distribution. The full text of the license may b= e found at +# http://opensource.org/licenses/bsd-license.php +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMP= LIED. +# + +''' +Pkcs7Sign +''' + +import os +import sys +import argparse +import subprocess +import uuid +import struct +import collections +from Common.BuildVersion import gBUILD_VERSION + +# +# Globals for help information +# +__prog__ =3D 'Pkcs7Sign' +__version__ =3D '%s Version %s' % (__prog__, '0.9 ' + gBUILD_VERSION) +__copyright__ =3D 'Copyright (c) 2016, Intel Corporation. All rights reser= ved.' +__usage__ =3D '%s -e|-d [options] ' % (__prog__) + +# +# GUID for PKCS7 from UEFI Specification +# +WIN_CERT_REVISION =3D 0x0200 +WIN_CERT_TYPE_EFI_GUID =3D 0x0EF1 +EFI_CERT_TYPE_PKCS7_GUID =3D uuid.UUID('{4aafd29d-68df-49ee-8aa9-347d37566= 5a7}') + +# +# typedef struct _WIN_CERTIFICATE { +# UINT32 dwLength; +# UINT16 wRevision; +# UINT16 wCertificateType; +# //UINT8 bCertificate[ANYSIZE_ARRAY]; +# } WIN_CERTIFICATE; +# +# typedef struct _WIN_CERTIFICATE_UEFI_GUID { +# WIN_CERTIFICATE Hdr; +# EFI_GUID CertType; +# //UINT8 CertData[ANYSIZE_ARRAY]; +# } WIN_CERTIFICATE_UEFI_GUID; +# +# typedef struct { +# UINT64 MonotonicCount; +# WIN_CERTIFICATE_UEFI_GUID AuthInfo; +# } EFI_FIRMWARE_IMAGE_AUTHENTICATION; +# + +# +# Filename of test signing private cert that is stored in same directory a= s this tool +# +TEST_SIGNER_PRIVATE_CERT_FILENAME =3D 'TestCert.pem' +TEST_OTHER_PUBLIC_CERT_FILENAME =3D 'TestSub.pub.pem' +TEST_TRUSTED_PUBLIC_CERT_FILENAME =3D 'TestRoot.pub.pem' + +if __name__ =3D=3D '__main__': + # + # Create command line argument parser object + # + parser =3D argparse.ArgumentParser(prog=3D__prog__, version=3D__version_= _, usage=3D__usage__, description=3D__copyright__, conflict_handler=3D'reso= lve') + group =3D parser.add_mutually_exclusive_group(required=3DTrue) + group.add_argument("-e", action=3D"store_true", dest=3D'Encode', help=3D= 'encode file') + group.add_argument("-d", action=3D"store_true", dest=3D'Decode', help=3D= 'decode file') + parser.add_argument("-o", "--output", dest=3D'OutputFile', type=3Dstr, m= etavar=3D'filename', help=3D"specify the output filename", required=3DTrue) + parser.add_argument("--signer-private-cert", dest=3D'SignerPrivateCertFi= le', type=3Dargparse.FileType('rb'), help=3D"specify the signer private cer= t filename. If not specified, a test signer private cert is used.") + parser.add_argument("--other-public-cert", dest=3D'OtherPublicCertFile',= type=3Dargparse.FileType('rb'), help=3D"specify the other public cert file= name. If not specified, a test other public cert is used.") + parser.add_argument("--trusted-public-cert", dest=3D'TrustedPublicCertFi= le', type=3Dargparse.FileType('rb'), help=3D"specify the trusted public cer= t filename. If not specified, a test trusted public cert is used.") + parser.add_argument("--monotonic-count", dest=3D'MonotonicCountStr', typ= e=3Dstr, help=3D"specify the MonotonicCount in FMP capsule. If not specifi= ed, 0 is used.") + parser.add_argument("--signature-size", dest=3D'SignatureSizeStr', type= =3Dstr, help=3D"specify the signature size for decode process.") + parser.add_argument("-v", "--verbose", dest=3D'Verbose', action=3D"store= _true", help=3D"increase output messages") + parser.add_argument("-q", "--quiet", dest=3D'Quiet', action=3D"store_tru= e", help=3D"reduce output messages") + parser.add_argument("--debug", dest=3D'Debug', type=3Dint, metavar=3D'[0= -9]', choices=3Drange(0,10), default=3D0, help=3D"set debug level") + parser.add_argument(metavar=3D"input_file", dest=3D'InputFile', type=3Da= rgparse.FileType('rb'), help=3D"specify the input filename") + + # + # Parse command line arguments + # + args =3D parser.parse_args() + + # + # Generate file path to Open SSL command + # + OpenSslCommand =3D 'openssl' + try: + OpenSslPath =3D os.environ['OPENSSL_PATH'] + OpenSslCommand =3D os.path.join(OpenSslPath, OpenSslCommand) + except: + pass + + # + # Verify that Open SSL command is available + # + try: + Process =3D subprocess.Popen('%s version' % (OpenSslCommand), stdout= =3Dsubprocess.PIPE, stderr=3Dsubprocess.PIPE) + except: + print 'ERROR: Open SSL command not available. Please verify PATH or s= et OPENSSL_PATH' + sys.exit(1) + + Version =3D Process.communicate() + if Process.returncode <> 0: + print 'ERROR: Open SSL command not available. Please verify PATH or s= et OPENSSL_PATH' + sys.exit(Process.returncode) + print Version[0] + + # + # Read input file into a buffer and save input filename + # + args.InputFileName =3D args.InputFile.name + args.InputFileBuffer =3D args.InputFile.read() + args.InputFile.close() + + # + # Save output filename and check if path exists + # + OutputDir =3D os.path.dirname(args.OutputFile) + if not os.path.exists(OutputDir): + print 'ERROR: The output path does not exist: %s' % OutputDir + sys.exit(1) + args.OutputFileName =3D args.OutputFile + + try: + if args.MonotonicCountStr.upper().startswith('0X'): + args.MonotonicCountValue =3D (long)(args.MonotonicCountStr, 16) + else: + args.MonotonicCountValue =3D (long)(args.MonotonicCountStr) + except: + args.MonotonicCountValue =3D (long)(0) + + if args.Encode: + # + # Save signer private cert filename and close private cert file + # + try: + args.SignerPrivateCertFileName =3D args.SignerPrivateCertFile.name + args.SignerPrivateCertFile.close() + except: + try: + # + # Get path to currently executing script or executable + # + if hasattr(sys, 'frozen'): + Pkcs7ToolPath =3D sys.executable + else: + Pkcs7ToolPath =3D sys.argv[0] + if Pkcs7ToolPath.startswith('"'): + Pkcs7ToolPath =3D Pkcs7ToolPath[1:] + if Pkcs7ToolPath.endswith('"'): + Pkcs7ToolPath =3D RsaToolPath[:-1] + args.SignerPrivateCertFileName =3D os.path.join(os.path.dirname(os= .path.realpath(Pkcs7ToolPath)), TEST_SIGNER_PRIVATE_CERT_FILENAME) + args.SignerPrivateCertFile =3D open(args.SignerPrivateCertFileName= , 'rb') + args.SignerPrivateCertFile.close() + except: + print 'ERROR: test signer private cert file %s missing' % (args.Si= gnerPrivateCertFileName) + sys.exit(1) + + # + # Save other public cert filename and close public cert file + # + try: + args.OtherPublicCertFileName =3D args.OtherPublicCertFile.name + args.OtherPublicCertFile.close() + except: + try: + # + # Get path to currently executing script or executable + # + if hasattr(sys, 'frozen'): + Pkcs7ToolPath =3D sys.executable + else: + Pkcs7ToolPath =3D sys.argv[0] + if Pkcs7ToolPath.startswith('"'): + Pkcs7ToolPath =3D Pkcs7ToolPath[1:] + if Pkcs7ToolPath.endswith('"'): + Pkcs7ToolPath =3D RsaToolPath[:-1] + args.OtherPublicCertFileName =3D os.path.join(os.path.dirname(os.p= ath.realpath(Pkcs7ToolPath)), TEST_OTHER_PUBLIC_CERT_FILENAME) + args.OtherPublicCertFile =3D open(args.OtherPublicCertFileName, 'r= b') + args.OtherPublicCertFile.close() + except: + print 'ERROR: test other public cert file %s missing' % (args.Othe= rPublicCertFileName) + sys.exit(1) + + format =3D "Q%ds" % len(args.InputFileBuffer) + FullInputFileBuffer =3D struct.pack(format,args.MonotonicCountValue, a= rgs.InputFileBuffer) + + # + # Sign the input file using the specified private key and capture sign= ature from STDOUT + # + Process =3D subprocess.Popen('%s smime -sign -binary -signer "%s" -out= form DER -md sha256 -certfile "%s"' % (OpenSslCommand, args.SignerPrivateCe= rtFileName, args.OtherPublicCertFileName), stdin=3Dsubprocess.PIPE, stdout= =3Dsubprocess.PIPE, stderr=3Dsubprocess.PIPE) + Signature =3D Process.communicate(input=3DFullInputFileBuffer)[0] + if Process.returncode <> 0: + sys.exit(Process.returncode) + + # + # Write output file that contains Signature, and Input data + # + args.OutputFile =3D open(args.OutputFileName, 'wb') + args.OutputFile.write(Signature) + args.OutputFile.write(args.InputFileBuffer) + args.OutputFile.close() + + if args.Decode: + # + # Save trusted public cert filename and close public cert file + # + try: + args.TrustedPublicCertFileName =3D args.TrustedPublicCertFile.name + args.TrustedPublicCertFile.close() + except: + try: + # + # Get path to currently executing script or executable + # + if hasattr(sys, 'frozen'): + Pkcs7ToolPath =3D sys.executable + else: + Pkcs7ToolPath =3D sys.argv[0] + if Pkcs7ToolPath.startswith('"'): + Pkcs7ToolPath =3D Pkcs7ToolPath[1:] + if Pkcs7ToolPath.endswith('"'): + Pkcs7ToolPath =3D RsaToolPath[:-1] + args.TrustedPublicCertFileName =3D os.path.join(os.path.dirname(os= .path.realpath(Pkcs7ToolPath)), TEST_TRUSTED_PUBLIC_CERT_FILENAME) + args.TrustedPublicCertFile =3D open(args.TrustedPublicCertFileName= , 'rb') + args.TrustedPublicCertFile.close() + except: + print 'ERROR: test trusted public cert file %s missing' % (args.Tr= ustedPublicCertFileName) + sys.exit(1) + + if not args.SignatureSizeStr: + print "ERROR: please use the option --signature-size to specify the = size of the signature data!" + sys.exit(1) + else: + if args.SignatureSizeStr.upper().startswith('0X'): + SignatureSize =3D (long)(args.SignatureSizeStr, 16) + else: + SignatureSize =3D (long)(args.SignatureSizeStr) + if SignatureSize < 0: + print "ERROR: The value of option --signature-size can't be set to= negative value!" + sys.exit(1) + elif SignatureSize > len(args.InputFileBuffer): + print "ERROR: The value of option --signature-size is exceed the s= ize of the input file !" + sys.exit(1) + + args.SignatureBuffer =3D args.InputFileBuffer[0:SignatureSize] + args.InputFileBuffer =3D args.InputFileBuffer[SignatureSize:] + + format =3D "Q%ds" % len(args.InputFileBuffer) + FullInputFileBuffer =3D struct.pack(format,args.MonotonicCountValue, a= rgs.InputFileBuffer) + + # + # Save output file contents from input file + # + open(args.OutputFileName, 'wb').write(FullInputFileBuffer) + + # + # Verify signature + # + Process =3D subprocess.Popen('%s smime -verify -inform DER -content %s= -CAfile %s' % (OpenSslCommand, args.OutputFileName, args.TrustedPublicCert= FileName), stdin=3Dsubprocess.PIPE, stdout=3Dsubprocess.PIPE, stderr=3Dsubp= rocess.PIPE) + Process.communicate(input=3Dargs.SignatureBuffer)[0] + if Process.returncode <> 0: + print 'ERROR: Verification failed' + os.remove (args.OutputFileName) + sys.exit(Process.returncode) + + open(args.OutputFileName, 'wb').write(args.InputFileBuffer) --=20 2.6.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel