From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web11.24091.1668875565159732384 for ; Sat, 19 Nov 2022 08:32:45 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=iVugvVjL; spf=pass (domain: intel.com, ip: 134.134.136.31, mailfrom: yi1.li@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1668875565; x=1700411565; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=aJLenFSJIRD6OE7/qqaEoEI+/NyoWhmS0OeZAYVESyw=; b=iVugvVjLpI+p6yHZEy4KU0V7C5+J8LXx21yUEPBxtgflYDdp/tsjN+dv 0fLL8BtOt8IQE4NWIPDE9TOYc0PW6RGrU224s0A7xJiBlnngqusF87uvA OZyx+XfVIBWutKF+au14+LZq6GnR6Z8CTcBx3xzll6pn76mcJIiuPS5xB s3WZwxs6+tKQtstlVDo+0T9gw4U8p+q8X3qJBqWxmqfKYS4XySC4Oljed fr9nu9vqQ6W2AM5+JGBIXsusfZqRyILaGDvMTYknB4jM3EAvdErM4lAgB EhTYcjCMv2rvxS9Oi/DzFVTWrhAwE8U+Y9i6VvHaF/Y603I+XtPxWvoVx Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10536"; a="375474612" X-IronPort-AV: E=Sophos;i="5.96,176,1665471600"; d="scan'208";a="375474612" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Nov 2022 08:32:44 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10536"; a="729561623" X-IronPort-AV: E=Sophos;i="5.96,176,1665471600"; d="scan'208";a="729561623" Received: from orsmsx602.amr.corp.intel.com ([10.22.229.15]) by FMSMGA003.fm.intel.com with ESMTP; 19 Nov 2022 08:32:44 -0800 Received: from orsmsx610.amr.corp.intel.com (10.22.229.23) by ORSMSX602.amr.corp.intel.com (10.22.229.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 19 Nov 2022 08:32:44 -0800 Received: from ORSEDG601.ED.cps.intel.com (10.7.248.6) by orsmsx610.amr.corp.intel.com (10.22.229.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31 via Frontend Transport; Sat, 19 Nov 2022 08:32:44 -0800 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (104.47.57.46) by edgegateway.intel.com (134.134.137.102) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2375.31; Sat, 19 Nov 2022 08:32:43 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aGoDjpUbb/5bJ1zxGalgiHAUqziJcvZkh3KQ5nyGkpI4b6u+pS0SYXj5U0n36LCEUlq21ac6KUINBs3hOBHFq80gcRiXQmmmOmKAyrJdS/G/CES6M/cGpmeCPUdCME+R7ukb4UpgjPp8KKnl2UAzoyBjLCAgciPV0CI3QvVo5S8+0yh9fu9JR/9mPrYvCAs4gSrvIFseYatWrZYs+Q2YSJebsnOflnl2Yw4BJUpAT8T3mYHTAEb9IrO/u+C4uRfaZ8FGn30qFbhX7tro+O9YPB6e0gaqmG2Qkl8dQ+kYQS4Z4UAFFeacHysz6AhWoSIBGoMV6S7CIhbP963tMK2Okw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=oornlB89IBc84EwzgDYLPXi4/r3BL4JETRsRoQRnyeY=; b=YGWTfKP3gTWwfUsKWklpE7S1NDcNI1Yn5N8Jyg6ontOQlzgOKurxL1tc7yyECIcGHjYIs7EpJI09Dvr5WJAZ3tODM5+Zi9TovcG4m1UgwRoG33/G0LwM4+6cb5CWiVJwbM8T3Z7ANhoCXMZRiX/8Ik5QGDkoB540aor7ysSyk73OCD48N+DBXJ+Z9dGXG36+Ms7VVKiHwRmT4x4Zny4aWZHWfjS0NXEBfRZ7CyBiWQkBt1AWHTS+aqL+L4hIQArtZzJeK40PBcgzl5QB/AjjfJf9OXB/E3LPE7UOqUlNDWG8kjU3HShuOpVmnjifX+8SVzjSB0Fc5L6sFVpylUBx3A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Received: from SJ1PR11MB6227.namprd11.prod.outlook.com (2603:10b6:a03:45a::10) by DM8PR11MB5654.namprd11.prod.outlook.com (2603:10b6:8:33::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5813.17; Sat, 19 Nov 2022 16:32:41 +0000 Received: from SJ1PR11MB6227.namprd11.prod.outlook.com ([fe80::221e:24cf:a864:9986]) by SJ1PR11MB6227.namprd11.prod.outlook.com ([fe80::221e:24cf:a864:9986%4]) with mapi id 15.20.5813.016; Sat, 19 Nov 2022 16:32:41 +0000 From: "Li, Yi" To: "devel@edk2.groups.io" CC: "Yao, Jiewen" , "Wang, Jian J" , "Lu, Xiaoyu1" , "Jiang, Guomin" Subject: Re: [PATCH] CryptoPkg: Add b_print.c which removed floating-point to OpensslLib Thread-Topic: [PATCH] CryptoPkg: Add b_print.c which removed floating-point to OpensslLib Thread-Index: AQHY/DPvyfZXN0Z4HE68Bs4Ea/6LG65Gb6RQ Date: Sat, 19 Nov 2022 16:32:41 +0000 Message-ID: References: <20221119162754.1034-1-yi1.li@intel.com> In-Reply-To: <20221119162754.1034-1-yi1.li@intel.com> Accept-Language: zh-CN, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: SJ1PR11MB6227:EE_|DM8PR11MB5654:EE_ x-ms-office365-filtering-correlation-id: 95044f97-d6ca-47ce-dac3-08daca4badec x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: S4hN3YISCFNCmLTHtyFzfiJWO9Kj/Kb45Uwyh3KBiz0lD0fs3VNVId6DO5NRclEDauGu0/7m7ecXUZuhlAnMyMp26yHdWrvcQCf2P1Wg2ozBuC880ymtT4H0utnpNKnmsbolGj4wtifvo8ERLo8k17JBL3f/IzSJxKdER4qWlDANI0uHk6bsv2Bn/0lFmSurUZ4EZNdlXNGq/QF6sEAyVtoRhcK1qLc4BXP67LvWVoPxw7cPUJDCLYFfrbdvOwm4RuhV0PV/GsjZmIQ+WQeiurEIYKzesJ9UrjpWIvNv/MS6z7Syf+38w20y36/mZAUKrNjupvF+dPgn+K35Q/lZ4zi8ghGe/cX4EYYUEvrMJIHMVAmWUGvZyIJuCRbLYSA7SiAKUOh/3SEqeYz6wgf2PndgysL0Pu2z4BdRD2HQWV91R1jg7In+hrXrG8KDjsWhBeHZiyFGt3F1fVdBSyTbGbjlML44ACbjh0hE9aIHyN+58rSOrRV9ylVc+WYyO0O2jHxGguq4LeDcg00csXqbA6SL9I6ODxTn0KlYs98CQKQSItQcndCjujHDADXKdKAk98/L4r/+lkTcigFj3vyDl6yJaEZxWmOZneetTc0UVlzzG8un/OAhIUW0mnI0JJw6q2yyTiUVEENy0GOItcgoUKXjigF9z2dyFhXs0lNLpiqySptGA4mLfUm0WOLDtz/qXW65txfqxmMizv1iVeeMfh0lu84CmRKfFQdlxwdtyjYP/zItg3DFr90eRT/Xi7iuNprWIUOIV48kk/ApODVLhk6pvgxi1OCOg9rShXxwjXY= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ1PR11MB6227.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(39860400002)(376002)(346002)(136003)(366004)(396003)(451199015)(186003)(2906002)(38100700002)(107886003)(66946007)(54906003)(64756008)(53546011)(66446008)(4326008)(82960400001)(8676002)(76116006)(33656002)(6916009)(7696005)(55016003)(66556008)(66476007)(316002)(71200400001)(9686003)(26005)(38070700005)(83380400001)(86362001)(966005)(478600001)(6506007)(52536014)(8936002)(41300700001)(5660300002)(122000001)(30864003)(21314003)(559001)(579004);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?/XvpsKYnosz+nJaoUmfOd6jiep7UKffvyF/r+KoS31oVMJY8/jKZNaa4QbC8?= =?us-ascii?Q?3JimGR7J9EYkBTU7S9+mqf+eoAwTxf83BeWTDOh1ma2g55zz8qotAesjwQOG?= =?us-ascii?Q?0TTJZKvRjUG+H2FLDwTbRG4bkbPaINmvANlAs9Np3n6d8t6V2cVzbGQAZCGm?= =?us-ascii?Q?IS4kJtxZp4FCaxgonY5eMDdvm5xhbnl7WOk8JTM2LhHV2LEhqcyNztXpiaKq?= =?us-ascii?Q?aVuHRdMCw/Lf8FmmKx0//GKJW7MCHUxPZt0YhLmio38KH+Aeia+oiIIRzqU1?= =?us-ascii?Q?TTx/0cl9XlAimj5FhHAOsxF4NF17+K2kZ3boM1cv48Gn9ZJKA5n7mpCow9pm?= =?us-ascii?Q?oOZPHD8Bp3yE2C9fUq8QR/Uh0jpcl0bauqisUQqk+xjW/HSD+Iw2bPeCY6W9?= =?us-ascii?Q?v70TOKRgjksT32IJiac5Fxs/7d+j5d20XBzRb/gVvd6qY2kjRwI2t6d+3EQU?= =?us-ascii?Q?R6Tlb5/zdARfMduyIYxiq4WGGgUgDJUYtb41BRFha+GR2XwvOup7k2lXh6Rj?= =?us-ascii?Q?BL1XIkNLhe5uj2VLug4JwkISE9llBQv2gYH4z65uv18CIBsLn/Rl9pyPtq0O?= =?us-ascii?Q?qqV/2sVgd2uM1z8IstzrUWZN9k5pDNNV6jQsJvb3wyj9wBc92RYHegUpdnGd?= =?us-ascii?Q?qPzGQ2dqfm+3R038JRBER63N/M86J3wh7iQot1jObz+hFpqlo0v//lOR0z4t?= =?us-ascii?Q?wkiLaybt73sGdsb16jcfYkiaDKooUByvXcsSfdl1u+QM5khm+WjfTlSH6i0K?= =?us-ascii?Q?5BL96mBNojHwn4Lli4D9RuoH8PoHPqTlsvHm3z12bSPICq5gA+yMNY3mLhhi?= =?us-ascii?Q?s/7m6RaZVAFsFF/vZJ08unGdQFDCDtJQJuThwGj4lPaeCPJdK45MGWjbuAv6?= =?us-ascii?Q?syfM2HbBiDSe4Jb0rtpT/hKHDU+4hb/VWIUWSx2KSQ2fZVPePb78+KHIHIGL?= =?us-ascii?Q?cCOf8kDh3771zrW5TMlYz9SQT8btqBBEIXOrIhms4RvvKtkWKGpSAj9Kn6cn?= =?us-ascii?Q?0myj3JHx49MXZ7xK2sfLAQ5/D2OsetpZsY0P8lNepf/Im/S+ZvXA0CfbI81v?= =?us-ascii?Q?v/U2Ry6l44bwCHzbO8q4OVWWNarwxcw3tUiGhn1SvKWoRIt2FmIW6xnusAsq?= =?us-ascii?Q?vmqzO4HlHInWb+1rZtKSBko03pfjhYFwuYvsOuMG7vrtmrs3h+CLdmm2bhaw?= =?us-ascii?Q?TAP0D3m89dhsET/PDvo+Apv/e4KTfiWBh4ziBIobe5rrW9GoTyJrDkz8Ik+R?= =?us-ascii?Q?G5zP+u426gDTR1EijzzR2iLuf3+XKtNnme/vwFi6K2r7GNfXVhuCzSmr4rCr?= =?us-ascii?Q?c2m8vOBjSEh61BkSO1kd/Lsyz3bPbl96Iq9Yy6nhwLjJv0wU20dcHtO6EkYt?= =?us-ascii?Q?yTOZpN12sGjJ2w8QnrSHW8lhsqwhArPPlk0ZV1GybZcWKhRWVeSLw7CQn5dT?= =?us-ascii?Q?Hp00nQULGWyiFQkzEM2WM4mXT3T7BAM7BiKSTKh16Y143z+X5o4KwiSE7Ygj?= =?us-ascii?Q?gpF8m09QbR5JjCsy1kt9cqhl91HjfPrBotFkvhM62l90cRIlbYRHhdyKqU9k?= =?us-ascii?Q?W/6Bg7h3mhvt5CK+4S4=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SJ1PR11MB6227.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 95044f97-d6ca-47ce-dac3-08daca4badec X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Nov 2022 16:32:41.5529 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: soF9zVRSF4hW7Enl/4Dfas8vuiUSYQx1Z5lLSDogmj3STf29g9i/hx5Wpsl5XULwpF6WKwa6UU61Y15ROeXCzg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM8PR11MB5654 Return-Path: yi1.li@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi all, This patch is the pre-work for enabling the X509 certificate time check. I know it looks weird to add a new b_print.c, so I wanted to explain the ba= ckground of this patch. 1. Why not directly use the b_print.c file in openssl? The original file uses code related to floating-point and ulldrvm, which wi= ll cause compilation errors like: unresolved external symbol __aulldvrm error: SSE register return with SSE disabled=20 ref: https://github.com/tianocore/edk2/pull/3508 So I removed all float code and replaced ulldrvm with DivU64x32Remainder(): 1).=20 case 'f': case 'E': case 'e': case 'G': case 'g': return -1; 2). + uvalue =3D DivU64x32Remainder(uvalue, (unsigned)base, &uremainder)= ; convert[place++] =3D (caps ? "0123456789ABCDEF" : "0123456789abcde= f") - [uvalue % (unsigned)base]; - uvalue =3D (uvalue / (unsigned)base); + [uremainder]; 2. Why not use the similar print function AsciiVSPrint in EDK2? EDK2 functions are all ms_abi but openssl functions not, so they use differ= ent va_list definitions, the relevant codes are as follows: GCC:*_*_X64_CC_FLAGS =3D ...... -DNO_MSABI_VA_FUNCS https://github.com/tianocore/edk2/blob/fff6d81270b57ee786ea18ad74f43149b9f0= 3494/CryptoPkg/Library/OpensslLib/OpensslLib.inf#LL636C2-L636C3 Therefore, segmentation fault will occur when calling the edk2 VA parameter= function in the openssl function. 3. Why not remove NO_MSABI_VA_FUNCS? Similarly, ms va_list cannot be used in the sysv function in openssl, which= will also cause segmentation fault. I am not expert of compilation, let me know if there is other better idea. Thanks, Yi -----Original Message----- From: Li, Yi1 =20 Sent: Sunday, November 20, 2022 12:28 AM To: devel@edk2.groups.io Cc: Li, Yi1 ; Yao, Jiewen ; Wang, J= ian J ; Lu, Xiaoyu1 ; Jiang, G= uomin Subject: [PATCH] CryptoPkg: Add b_print.c which removed floating-point to O= pensslLib REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4110 Openssl will use BIO_snprintf() to print time string when converting time_t= to ASN1_time. Currently edk2 code just give it a NULL implement in CrtWrapper.c, so getti= ng current time in X509 time check will be fail. This patch add a copy of OpensslLib\openssl\crypto\bio\b_print.c to Openssl= Lib, with below changes: 1. All floating point related code removed. 2. Replace ull divide and remainder with DivU64x32Remainder(). Cc: Jiewen Yao Cc: Jian J Wang Cc: Xiaoyu Lu Cc: Guomin Jiang Signed-off-by: Yi Li --- CryptoPkg/CryptoPkg.ci.yaml | 4 +- .../Library/BaseCryptLib/SysCall/CrtWrapper.c | 27 - .../SysCall/UnitTestHostCrtWrapper.c | 25 - CryptoPkg/Library/OpensslLib/OpensslLib.inf | 1 + .../Library/OpensslLib/OpensslLibAccel.inf | 1 + .../Library/OpensslLib/OpensslLibCrypto.inf | 1 + .../Library/OpensslLib/OpensslLibFull.inf | 1 + .../OpensslLib/OpensslLibFullAccel.inf | 1 + CryptoPkg/Library/OpensslLib/b_print.c | 613 ++++++++++++++++++ 9 files changed, 621 insertions(+), 53 deletions(-) create mode 100644 Cr= yptoPkg/Library/OpensslLib/b_print.c diff --git a/CryptoPkg/CryptoPkg.ci.yaml b/CryptoPkg/CryptoPkg.ci.yaml inde= x 47f2975967..8c25f581fb 100644 --- a/CryptoPkg/CryptoPkg.ci.yaml +++ b/CryptoPkg/CryptoPkg.ci.yaml @@ -13,7 +13,9 @@ "Library/OpensslLib/IA32Gcc", "Library/OpensslLib/X64", "Library/OpensslLib/X64Gcc", - "Library/Include/openssl" + "Library/Include/openssl", + # b_print.c is a copy from OpenSSl. + "Library/OpensslLib/b_print.c" ] }, "EccCheck": { diff --git a/CryptoPkg/Library/BaseCryptLib/SysCall/CrtWrapper.c b/CryptoPk= g/Library/BaseCryptLib/SysCall/CrtWrapper.c index b65d29485b..6d7ac3efdc 100644 --- a/CryptoPkg/Library/BaseCryptLib/SysCall/CrtWrapper.c +++ b/CryptoPkg/Library/BaseCryptLib/SysCall/CrtWrapper.c @@ -472,33 +472,6 @@ fwrite ( return 0; } =20 -// -// -- Dummy OpenSSL Support Routines -- -// - -int -BIO_printf ( - void *bio, - const char *format, - ... - ) -{ - return 0; -} - -int -BIO_snprintf ( - char *buf, - size_t n, - const char *format, - ... - ) -{ - // Because the function does not actually print anything to buf, it retu= rns -1 as error. - // Otherwise, the consumer may think that the buf is valid and parse the= buffer. - return -1; -} - #ifdef __GNUC__ =20 typedef diff --git a/CryptoPkg/Library/BaseCryptLib/SysCall/UnitTestHostCrtWrapper.= c b/CryptoPkg/Library/BaseCryptLib/SysCall/UnitTestHostCrtWrapper.c index 066d53e4fa..244e57437e 100644 --- a/CryptoPkg/Library/BaseCryptLib/SysCall/UnitTestHostCrtWrapper.c +++ b/CryptoPkg/Library/BaseCryptLib/SysCall/UnitTestHostCrtWrapper.c @@ -72,31 +72,6 @@ sscanf ( return 0; } =20 -// -// -- Dummy OpenSSL Support Routines -- -// - -int -BIO_printf ( - void *bio, - const char *format, - ... - ) -{ - return 0; -} - -int -BIO_snprintf ( - char *buf, - size_t n, - const char *format, - ... - ) -{ - return 0; -} - uid_t getuid ( void diff --git a/CryptoPkg/Library/OpensslLib/OpensslLib.inf b/CryptoPkg/Librar= y/OpensslLib/OpensslLib.inf index 60c6c24b0a..f8ddfadc51 100644 --- a/CryptoPkg/Library/OpensslLib/OpensslLib.inf +++ b/CryptoPkg/Library/OpensslLib/OpensslLib.inf @@ -580,6 +580,7 @@ rand_pool.c # SslNull.c EcSm2Null.c + b_print.c =20 [Packages] MdePkg/MdePkg.dec diff --git a/CryptoPkg/Library/OpensslLib/OpensslLibAccel.inf b/CryptoPkg/L= ibrary/OpensslLib/OpensslLibAccel.inf index 103ef7bda2..6ee7bfd329 100644 --- a/CryptoPkg/Library/OpensslLib/OpensslLibAccel.inf +++ b/CryptoPkg/Library/OpensslLib/OpensslLibAccel.inf @@ -580,6 +580,7 @@ rand_pool.c # SslNull.c EcSm2Null.c + b_print.c =20 [Sources.IA32] # Autogenerated IA32 files list starts here diff --git a/CryptoPkg/Library= /OpensslLib/OpensslLibCrypto.inf b/CryptoPkg/Library/OpensslLib/OpensslLibC= rypto.inf index c4eaea888c..3e43cf146b 100644 --- a/CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf +++ b/CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf @@ -530,6 +530,7 @@ rand_pool.c SslNull.c EcSm2Null.c + b_print.c =20 [Packages] MdePkg/MdePkg.dec diff --git a/CryptoPkg/Library/OpensslLib/OpensslLibFull.inf b/CryptoPkg/Li= brary/OpensslLib/OpensslLibFull.inf index 309e43055c..440d3e7e1d 100644 --- a/CryptoPkg/Library/OpensslLib/OpensslLibFull.inf +++ b/CryptoPkg/Library/OpensslLib/OpensslLibFull.inf @@ -635,6 +635,7 @@ rand_pool.c # SslNull.c # EcSm2Null.c + b_print.c =20 [Packages] MdePkg/MdePkg.dec diff --git a/CryptoPkg/Library/OpensslLib/OpensslLibFullAccel.inf b/CryptoP= kg/Library/OpensslLib/OpensslLibFullAccel.inf index 4eeeeb79bd..413f70f733 100644 --- a/CryptoPkg/Library/OpensslLib/OpensslLibFullAccel.inf +++ b/CryptoPkg/Library/OpensslLib/OpensslLibFullAccel.inf @@ -635,6 +635,7 @@ rand_pool.c # SslNull.c # EcSm2Null.c + b_print.c =20 [Sources.IA32] # Autogenerated IA32 files list starts here diff --git a/CryptoPkg/Library= /OpensslLib/b_print.c b/CryptoPkg/Library/OpensslLib/b_print.c new file mode 100644 index 0000000000..885c3b9264 --- /dev/null +++ b/CryptoPkg/Library/OpensslLib/b_print.c @@ -0,0 +1,613 @@ +/* + * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a=20 +copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + * + * Please notes: + * This file is a copy of OpensslLib\openssl\crypto\bio\b_print.c + * with all floating point related code removed (Not supported in UEFI). + * This means that '%f' '%g' '%e' and related combined formats are no long= er supported. + * + */ + +#include +#include +#include "internal/cryptlib.h" +#include "crypto/ctype.h" +#include "internal/numbers.h" +#include + +/* + * Copyright Patrick Powell 1995 + * This code is based on code written by Patrick Powell=20 + + * It may be used for any purpose as long as this notice remains intact + * on all source code distributions. + */ + +static int fmtstr(char **, char **, size_t *, size_t *, + const char *, int, int, int); static int fmtint(char=20 +**, char **, size_t *, size_t *, + int64_t, int, int, int, int); static int=20 +doapr_outch(char **, char **, size_t *, size_t *, int); static int=20 +_dopr(char **sbuffer, char **buffer, + size_t *maxlen, size_t *retlen, int *truncated, + const char *format, va_list args); + +/* format read states */ +#define DP_S_DEFAULT 0 +#define DP_S_FLAGS 1 +#define DP_S_MIN 2 +#define DP_S_DOT 3 +#define DP_S_MAX 4 +#define DP_S_MOD 5 +#define DP_S_CONV 6 +#define DP_S_DONE 7 + +/* format flags - Bits */ +/* left-aligned padding */ +#define DP_F_MINUS (1 << 0) +/* print an explicit '+' for a value with positive sign */ +#define DP_F_PLUS (1 << 1) +/* print an explicit ' ' for a value with positive sign */ +#define DP_F_SPACE (1 << 2) +/* print 0/0x prefix for octal/hex and decimal point for floating point */ +#define DP_F_NUM (1 << 3) +/* print leading zeroes */ +#define DP_F_ZERO (1 << 4) +/* print HEX in UPPPERcase */ +#define DP_F_UP (1 << 5) +/* treat value as unsigned */ +#define DP_F_UNSIGNED (1 << 6) + +/* conversion flags */ +#define DP_C_SHORT 1 +#define DP_C_LONG 2 +// #define DP_C_LDOUBLE 3 +#define DP_C_LLONG 4 +#define DP_C_SIZE 5 + +/* Floating point formats */ +// #define F_FORMAT 0 +// #define E_FORMAT 1 +// #define G_FORMAT 2 + +/* some handy macros */ +#define char_to_int(p) (p - '0') +#define OSSL_MAX(p,q) ((p >=3D q) ? p : q) + +static int +_dopr(char **sbuffer, + char **buffer, + size_t *maxlen, + size_t *retlen, int *truncated, const char *format, va_list args)=20 +{ + char ch; + int64_t value; + char *strvalue; + int min; + int max; + int state; + int flags; + int cflags; + size_t currlen; + + state =3D DP_S_DEFAULT; + flags =3D currlen =3D cflags =3D min =3D 0; + max =3D -1; + ch =3D *format++; + + while (state !=3D DP_S_DONE) { + if (ch =3D=3D '\0' || (buffer =3D=3D NULL && currlen >=3D *maxlen)= ) + state =3D DP_S_DONE; + + switch (state) { + case DP_S_DEFAULT: + if (ch =3D=3D '%') + state =3D DP_S_FLAGS; + else + if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch)) + return 0; + ch =3D *format++; + break; + case DP_S_FLAGS: + switch (ch) { + case '-': + flags |=3D DP_F_MINUS; + ch =3D *format++; + break; + case '+': + flags |=3D DP_F_PLUS; + ch =3D *format++; + break; + case ' ': + flags |=3D DP_F_SPACE; + ch =3D *format++; + break; + case '#': + flags |=3D DP_F_NUM; + ch =3D *format++; + break; + case '0': + flags |=3D DP_F_ZERO; + ch =3D *format++; + break; + default: + state =3D DP_S_MIN; + break; + } + break; + case DP_S_MIN: + if (ossl_isdigit(ch)) { + min =3D 10 * min + char_to_int(ch); + ch =3D *format++; + } else if (ch =3D=3D '*') { + min =3D va_arg(args, int); + ch =3D *format++; + state =3D DP_S_DOT; + } else + state =3D DP_S_DOT; + break; + case DP_S_DOT: + if (ch =3D=3D '.') { + state =3D DP_S_MAX; + ch =3D *format++; + } else + state =3D DP_S_MOD; + break; + case DP_S_MAX: + if (ossl_isdigit(ch)) { + if (max < 0) + max =3D 0; + max =3D 10 * max + char_to_int(ch); + ch =3D *format++; + } else if (ch =3D=3D '*') { + max =3D va_arg(args, int); + ch =3D *format++; + state =3D DP_S_MOD; + } else + state =3D DP_S_MOD; + break; + case DP_S_MOD: + switch (ch) { + case 'h': + cflags =3D DP_C_SHORT; + ch =3D *format++; + break; + case 'l': + if (*format =3D=3D 'l') { + cflags =3D DP_C_LLONG; + format++; + } else + cflags =3D DP_C_LONG; + ch =3D *format++; + break; + case 'q': + case 'j': + cflags =3D DP_C_LLONG; + ch =3D *format++; + break; + case 'L': + //Unsupported fmt in UEFI + return -1; + case 'z': + cflags =3D DP_C_SIZE; + ch =3D *format++; + break; + default: + break; + } + state =3D DP_S_CONV; + break; + case DP_S_CONV: + switch (ch) { + case 'd': + case 'i': + switch (cflags) { + case DP_C_SHORT: + value =3D (short int)va_arg(args, int); + break; + case DP_C_LONG: + value =3D va_arg(args, long int); + break; + case DP_C_LLONG: + value =3D va_arg(args, int64_t); + break; + case DP_C_SIZE: + value =3D va_arg(args, ossl_ssize_t); + break; + default: + value =3D va_arg(args, int); + break; + } + if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, = min, + max, flags)) + return 0; + break; + case 'X': + flags |=3D DP_F_UP; + /* FALLTHROUGH */ + case 'x': + case 'o': + case 'u': + flags |=3D DP_F_UNSIGNED; + switch (cflags) { + case DP_C_SHORT: + value =3D (unsigned short int)va_arg(args, unsigned in= t); + break; + case DP_C_LONG: + value =3D va_arg(args, unsigned long int); + break; + case DP_C_LLONG: + value =3D va_arg(args, uint64_t); + break; + case DP_C_SIZE: + value =3D va_arg(args, size_t); + break; + default: + value =3D va_arg(args, unsigned int); + break; + } + if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, + ch =3D=3D 'o' ? 8 : (ch =3D=3D 'u' ? 10 : 16), + min, max, flags)) + return 0; + break; + case 'f': + case 'E': + case 'e': + case 'G': + case 'g': + return -1; + case 'c': + if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, + va_arg(args, int))) + return 0; + break; + case 's': + strvalue =3D va_arg(args, char *); + if (max < 0) { + if (buffer) + max =3D INT_MAX; + else + max =3D *maxlen; + } + if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue, + flags, min, max)) + return 0; + break; + case 'p': + value =3D (size_t)va_arg(args, void *); + if (!fmtint(sbuffer, buffer, &currlen, maxlen, + value, 16, min, max, flags | DP_F_NUM)) + return 0; + break; + case 'n': + { + int *num; + num =3D va_arg(args, int *); + *num =3D currlen; + } + break; + case '%': + if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch)) + return 0; + break; + case 'w': + /* not supported yet, treat as next char */ + ch =3D *format++; + break; + default: + /* unknown, skip */ + break; + } + ch =3D *format++; + state =3D DP_S_DEFAULT; + flags =3D cflags =3D min =3D 0; + max =3D -1; + break; + case DP_S_DONE: + break; + default: + break; + } + } + /* + * We have to truncate if there is no dynamic buffer and we have fille= d the + * static buffer. + */ + if (buffer =3D=3D NULL) { + *truncated =3D (currlen > *maxlen - 1); + if (*truncated) + currlen =3D *maxlen - 1; + } + if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0')) + return 0; + *retlen =3D currlen - 1; + return 1; +} + +static int +fmtstr(char **sbuffer, + char **buffer, + size_t *currlen, + size_t *maxlen, const char *value, int flags, int min, int max)=20 +{ + int padlen; + size_t strln; + int cnt =3D 0; + + if (value =3D=3D 0) + value =3D ""; + + strln =3D OPENSSL_strnlen(value, max < 0 ? SIZE_MAX : (size_t)max); + + padlen =3D min - strln; + if (min < 0 || padlen < 0) + padlen =3D 0; + if (max >=3D 0) { + /* + * Calculate the maximum output including padding. + * Make sure max doesn't overflow into negativity + */ + if (max < INT_MAX - padlen) + max +=3D padlen; + else + max =3D INT_MAX; + } + if (flags & DP_F_MINUS) + padlen =3D -padlen; + + while ((padlen > 0) && (max < 0 || cnt < max)) { + if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) + return 0; + --padlen; + ++cnt; + } + while (strln > 0 && (max < 0 || cnt < max)) { + if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++)) + return 0; + --strln; + ++cnt; + } + while ((padlen < 0) && (max < 0 || cnt < max)) { + if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) + return 0; + ++padlen; + ++cnt; + } + return 1; +} + +static int +fmtint(char **sbuffer, + char **buffer, + size_t *currlen, + size_t *maxlen, int64_t value, int base, int min, int max, int=20 +flags) { + int signvalue =3D 0; + const char *prefix =3D ""; + uint64_t uvalue; + char convert[DECIMAL_SIZE(value) + 3]; + int place =3D 0; + int spadlen =3D 0; + int zpadlen =3D 0; + int caps =3D 0; + uint32_t uremainder =3D 0; + + if (max < 0) + max =3D 0; + uvalue =3D value; + if (!(flags & DP_F_UNSIGNED)) { + if (value < 0) { + signvalue =3D '-'; + uvalue =3D 0 - (uint64_t)value; + } else if (flags & DP_F_PLUS) + signvalue =3D '+'; + else if (flags & DP_F_SPACE) + signvalue =3D ' '; + } + if (flags & DP_F_NUM) { + if (base =3D=3D 8) + prefix =3D "0"; + if (base =3D=3D 16) + prefix =3D "0x"; + } + if (flags & DP_F_UP) + caps =3D 1; + do { + uvalue =3D DivU64x32Remainder(uvalue, (unsigned)base, &uremainder)= ; + convert[place++] =3D (caps ? "0123456789ABCDEF" : "0123456789abcde= f") + [uremainder]; + } while (uvalue && (place < (int)sizeof(convert))); + if (place =3D=3D sizeof(convert)) + place--; + convert[place] =3D 0; + + zpadlen =3D max - place; + spadlen =3D + min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix); + if (zpadlen < 0) + zpadlen =3D 0; + if (spadlen < 0) + spadlen =3D 0; + if (flags & DP_F_ZERO) { + zpadlen =3D OSSL_MAX(zpadlen, spadlen); + spadlen =3D 0; + } + if (flags & DP_F_MINUS) + spadlen =3D -spadlen; + + /* spaces */ + while (spadlen > 0) { + if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) + return 0; + --spadlen; + } + + /* sign */ + if (signvalue) + if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue)) + return 0; + + /* prefix */ + while (*prefix) { + if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix)) + return 0; + prefix++; + } + + /* zeros */ + if (zpadlen > 0) { + while (zpadlen > 0) { + if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0')) + return 0; + --zpadlen; + } + } + /* digits */ + while (place > 0) { + if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place= ])) + return 0; + } + + /* left justified spaces */ + while (spadlen < 0) { + if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) + return 0; + ++spadlen; + } + return 1; +} + +#define BUFFER_INC 1024 + +static int +doapr_outch(char **sbuffer, + char **buffer, size_t *currlen, size_t *maxlen, int c) { + /* If we haven't at least one buffer, someone has done a big booboo */ + if (!ossl_assert(*sbuffer !=3D NULL || buffer !=3D NULL)) + return 0; + + /* |currlen| must always be <=3D |*maxlen| */ + if (!ossl_assert(*currlen <=3D *maxlen)) + return 0; + + if (buffer && *currlen =3D=3D *maxlen) { + if (*maxlen > INT_MAX - BUFFER_INC) + return 0; + + *maxlen +=3D BUFFER_INC; + if (*buffer =3D=3D NULL) { + if ((*buffer =3D OPENSSL_malloc(*maxlen)) =3D=3D NULL) { + BIOerr(BIO_F_DOAPR_OUTCH, ERR_R_MALLOC_FAILURE); + return 0; + } + if (*currlen > 0) { + if (!ossl_assert(*sbuffer !=3D NULL)) + return 0; + memcpy(*buffer, *sbuffer, *currlen); + } + *sbuffer =3D NULL; + } else { + char *tmpbuf; + tmpbuf =3D OPENSSL_realloc(*buffer, *maxlen); + if (tmpbuf =3D=3D NULL) + return 0; + *buffer =3D tmpbuf; + } + } + + if (*currlen < *maxlen) { + if (*sbuffer) + (*sbuffer)[(*currlen)++] =3D (char)c; + else + (*buffer)[(*currlen)++] =3D (char)c; + } + + return 1; +} + +/********************************************************************** +*****/ + +int BIO_printf(BIO *bio, const char *format, ...) { + va_list args; + int ret; + + va_start(args, format); + + ret =3D BIO_vprintf(bio, format, args); + + va_end(args); + return ret; +} + +int BIO_vprintf(BIO *bio, const char *format, va_list args) { + int ret; + size_t retlen; + char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonab= le + * in small-stack environments, like threa= ds + * or DOS programs. */ + char *hugebufp =3D hugebuf; + size_t hugebufsize =3D sizeof(hugebuf); + char *dynbuf =3D NULL; + int ignored; + + dynbuf =3D NULL; + if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format= , + args)) { + OPENSSL_free(dynbuf); + return -1; + } + if (dynbuf) { + ret =3D BIO_write(bio, dynbuf, (int)retlen); + OPENSSL_free(dynbuf); + } else { + ret =3D BIO_write(bio, hugebuf, (int)retlen); + } + return ret; +} + +/* + * As snprintf is not available everywhere, we provide our own + * implementation. This function has nothing to do with BIOs, but it's + * closely related to BIO_printf, and we need *some* name prefix ...=20 +(XXX the + * function should be renamed, but to what?) */ int BIO_snprintf(char=20 +*buf, size_t n, const char *format, ...) { + va_list args; + int ret; + + va_start(args, format); + + ret =3D BIO_vsnprintf(buf, n, format, args); + + va_end(args); + return ret; +} + +int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list=20 +args) { + size_t retlen; + int truncated; + + if (!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args)) + return -1; + + if (truncated) + /* + * In case of truncation, return -1 like traditional snprintf. + * (Current drafts for ISO/IEC 9899 say snprintf should return the + * number of characters that would have been written, had the buff= er + * been large enough.) + */ + return -1; + else + return (retlen <=3D INT_MAX) ? (int)retlen : -1; } -- 2.31.1.windows.1