Repository: microsoft/mu_basecore
Pull request #1355: Add X509CompareDateTime Function
Author: login: liqiqiii
Labels: name: type:backport
description: Backport changes in a dev branch PR to its release branch.
Created: 2025-05-08T14:19:17Z | Merged: 2025-05-15T15:46:33Z

## Description

Add X509CompareDateTime function.

This function was missed before because it has a return type of INT32 which is not included in current generate_cryptodriver.py.

- [x] Impacts functionality?
- [ ] Impacts security?
- [ ] Breaking change?
- [ ] Includes tests?
- [ ] Includes documentation?
- [x] Backport to release branch?

## How This Was Tested

Tested with X64 STANDARD/TINY_SHA, build successfully.

## Integration Instructions

When integrating this, consume the latest MU_CRYPTO_RELEASE [PR](https://github.com/microsoft/mu_crypto_release/pull/132) too.


Diff:
diff --git a/CryptoPkg/CryptoPkg.dec b/CryptoPkg/CryptoPkg.dec
index d249bcaab5d..a07da4f8acb 100644
--- a/CryptoPkg/CryptoPkg.dec
+++ b/CryptoPkg/CryptoPkg.dec
@@ -292,6 +292,7 @@
   gEfiCryptoPkgTokenSpaceGuid.PcdCryptoServiceEcGetPublicKeyFromX509|FALSE|BOOLEAN|0xcc
   gEfiCryptoPkgTokenSpaceGuid.PcdCryptoServiceEcDsaSign|FALSE|BOOLEAN|0xcd
   gEfiCryptoPkgTokenSpaceGuid.PcdCryptoServiceEcDsaVerify|FALSE|BOOLEAN|0xce
+  gEfiCryptoPkgTokenSpaceGuid.PcdCryptoServiceX509CompareDateTime|FALSE|BOOLEAN|0xcf
 # AUTOGEN ENDS
 # ****************************************************************************
 # MU_CHANGE [END]
diff --git a/CryptoPkg/Include/Protocol/Crypto.h b/CryptoPkg/Include/Protocol/Crypto.h
index 5782595acb8..d93a00a9fa9 100644
--- a/CryptoPkg/Include/Protocol/Crypto.h
+++ b/CryptoPkg/Include/Protocol/Crypto.h
@@ -20,7 +20,7 @@
 /// the EDK II Crypto Protocol is extended, this version define must be
 /// increased.
 ///
-#define EDKII_CRYPTO_VERSION  18 // MU_CHANGE
+#define EDKII_CRYPTO_VERSION  19 // MU_CHANGE
 
 ///
 /// EDK II Crypto Protocol forward declaration
@@ -44,7 +44,7 @@ UINTN
 // AUTOGENERATED BY CryptoBinPkg\Driver\Packaging\generate_cryptodriver.py
 // AUTOGENED AS temp_Crypto.h
 // DO NOT MODIFY
-// GENERATED ON: 2024-04-08 11:12:11.528450
+// GENERATED ON: 2025-05-08 21:32:24.776904
 
 // =============================================================================
 //     HMACSHA256 functions
@@ -2295,6 +2295,27 @@ BOOLEAN
   IN OUT UINTN      *DateTimeSize
   );
 
+/**
+  Compare DateTime1 object and DateTime2 object.
+  If DateTime1 is NULL, then return -2.
+  If DateTime2 is NULL, then return -2.
+  If DateTime1 == DateTime2, then return 0
+  If DateTime1 > DateTime2, then return 1
+  If DateTime1 < DateTime2, then return -1
+  @param[in]      DateTime1         Pointer to a DateTime Ojbect
+  @param[in]      DateTime2         Pointer to a DateTime Object
+  @retval  0      If DateTime1 == DateTime2
+  @retval  1      If DateTime1 > DateTime2
+  @retval  -1     If DateTime1 < DateTime2
+**/
+// FROM BaseCryptLib.h:2817
+typedef
+INT32
+(EFIAPI *EDKII_CRYPTO_X509_COMPARE_DATE_TIME)(
+  IN  CONST  VOID  *DateTime1,
+  IN  CONST  VOID  *DateTime2
+  );
+
 /**
   Retrieve the Key Usage from one X.509 certificate.
   @param[in]      Cert             Pointer to the DER-encoded X509 certificate.
@@ -4772,6 +4793,7 @@ struct _EDKII_CRYPTO_PROTOCOL {
   EDKII_CRYPTO_EC_GET_PUBLIC_KEY_FROM_X509            EcGetPublicKeyFromX509;
   EDKII_CRYPTO_EC_DSA_SIGN                            EcDsaSign;
   EDKII_CRYPTO_EC_DSA_VERIFY                          EcDsaVerify;
+  EDKII_CRYPTO_X509_COMPARE_DATE_TIME                 X509CompareDateTime;
 };
 
 // AUTOGEN ENDS
diff --git a/CryptoPkg/Library/BaseCryptLibOnProtocolPpi/CryptLib.c b/CryptoPkg/Library/BaseCryptLibOnProtocolPpi/CryptLib.c
index b8b7456df4d..314c27ca9f1 100644
--- a/CryptoPkg/Library/BaseCryptLibOnProtocolPpi/CryptLib.c
+++ b/CryptoPkg/Library/BaseCryptLibOnProtocolPpi/CryptLib.c
@@ -100,7 +100,7 @@ CryptoServiceNotAvailable (
 // AUTOGENERATED BY CryptoBinPkg\Driver\Packaging\generate_cryptodriver.py
 // AUTOGENED AS temp_CryptLib.c
 // DO NOT MODIFY
-// GENERATED ON: 2024-04-08 11:12:11.608999
+// GENERATED ON: 2025-05-08 21:32:26.186748
 
 // =============================================================================
 //     HMACSHA256 functions
@@ -5212,6 +5212,30 @@ EcDsaVerify (
   CALL_CRYPTO_SERVICE (EcDsaVerify, (EcContext, HashNid, MessageHash, HashSize, Signature, SigSize), FALSE);
 }
 
+/**
+  Compare DateTime1 object and DateTime2 object.
+  If DateTime1 is NULL, then return -2.
+  If DateTime2 is NULL, then return -2.
+  If DateTime1 == DateTime2, then return 0
+  If DateTime1 > DateTime2, then return 1
+  If DateTime1 < DateTime2, then return -1
+  @param[in]      DateTime1         Pointer to a DateTime Ojbect
+  @param[in]      DateTime2         Pointer to a DateTime Object
+  @retval  0      If DateTime1 == DateTime2
+  @retval  1      If DateTime1 > DateTime2
+  @retval  -1     If DateTime1 < DateTime2
+**/
+// See BaseCryptLib.h:2817
+INT32
+EFIAPI
+X509CompareDateTime (
+  IN  CONST  VOID  *DateTime1,
+  IN  CONST  VOID  *DateTime2
+  )
+{
+  CALL_CRYPTO_SERVICE (X509CompareDateTime, (DateTime1, DateTime2), 0);
+}
+
 // AUTOGEN ENDS
 // ****************************************************************************
 // MU_CHANGE [END]
diff --git a/CryptoPkg/Library/BaseCryptLibOnProtocolPpi/DxeCryptLib.inf b/CryptoPkg/Library/BaseCryptLibOnProtocolPpi/DxeCryptLib.inf
index 1803fe2d795..db8df41986d 100644
--- a/CryptoPkg/Library/BaseCryptLibOnProtocolPpi/DxeCryptLib.inf
+++ b/CryptoPkg/Library/BaseCryptLibOnProtocolPpi/DxeCryptLib.inf
@@ -141,6 +141,7 @@
   gEfiCryptoPkgTokenSpaceGuid.PcdCryptoServiceX509GetExtensionData # CONSUMES
   gEfiCryptoPkgTokenSpaceGuid.PcdCryptoServiceX509GetValidity # CONSUMES
   gEfiCryptoPkgTokenSpaceGuid.PcdCryptoServiceX509FormatDateTime # CONSUMES
+  gEfiCryptoPkgTokenSpaceGuid.PcdCryptoServiceX509CompareDateTime # CONSUMES
   gEfiCryptoPkgTokenSpaceGuid.PcdCryptoServiceX509GetKeyUsage # CONSUMES
   gEfiCryptoPkgTokenSpaceGuid.PcdCryptoServiceX509GetExtendedKeyUsage # CONSUMES
   gEfiCryptoPkgTokenSpaceGuid.PcdCryptoServiceX509VerifyCertChain # CONSUMES


Reviews:
user: login: makubacki
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-08T15:53:34Z
user: login: liqiqiii
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-09T00:36:16Z
user: login: makubacki
state: APPROVED
author_association: MEMBER
submitted_at: 2025-05-09T00:56:17Z
user: login: apop5
state: APPROVED
author_association: COLLABORATOR
submitted_at: 2025-05-09T01:19:39Z
user: login: kenlautner
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-05-15T00:25:56Z

Review comments:
diff_hunk: @@ -4649,6 +4670,7 @@ struct _EDKII_CRYPTO_PROTOCOL {
   EDKII_CRYPTO_X509_GET_EXTENSION_DATA                X509GetExtensionData;
   EDKII_CRYPTO_X509_GET_VALIDITY                      X509GetValidity;
   EDKII_CRYPTO_X509_FORMAT_DATE_TIME                  X509FormatDateTime;
+  EDKII_CRYPTO_X509_COMPARE_DATE_TIME                 X509CompareDateTime;
path: CryptoPkg/Include/Protocol/Crypto.h
user: login: makubacki
body: I know this is roughly where the function sits in the protocol interface in [edk2](https://github.com/tianocore/edk2/blob/master/CryptoPkg/Private/Protocol/Crypto.h), but placing it here makes a breaking change for the crypto protocol as it is defined in Project Mu (which is already a different binary interface that that in edk2).

So, I'd propose this be placed at the end of the structure to at least maintain backward compatibility within the Project Mu protocol interface definition.

The protocol version should also be updated to reflect that this new function is available.
created_at: 2025-05-08T15:53:33Z
author_association: MEMBER
diff_hunk: @@ -4649,6 +4670,7 @@ struct _EDKII_CRYPTO_PROTOCOL {
   EDKII_CRYPTO_X509_GET_EXTENSION_DATA                X509GetExtensionData;
   EDKII_CRYPTO_X509_GET_VALIDITY                      X509GetValidity;
   EDKII_CRYPTO_X509_FORMAT_DATE_TIME                  X509FormatDateTime;
+  EDKII_CRYPTO_X509_COMPARE_DATE_TIME                 X509CompareDateTime;
path: CryptoPkg/Include/Protocol/Crypto.h
user: login: liqiqiii
body: I have made changes based on your comments and updated the protocol version. Thanks Michael.
created_at: 2025-05-09T00:36:16Z
author_association: CONTRIBUTOR

PR comments:
user: login: codecov-commenter
created_at: 2025-05-08T14:26:25Z
body: ## [Codecov](https://app.codecov.io/gh/microsoft/mu_basecore/pull/1355?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=microsoft) Report
:white_check_mark: All modified and coverable lines are covered by tests.
:warning: Please [upload](https://docs.codecov.com/docs/codecov-uploader) report for BASE (`dev/202502@7a9ec45`). [Learn more](https://docs.codecov.io/docs/error-reference?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=microsoft#section-missing-base-commit) about missing BASE report.

<details><summary>Additional details and impacted files</summary>



```diff
@@             Coverage Diff              @@
##             dev/202502   #1355   +/-   ##
============================================
  Coverage              ?   1.72%           
============================================
  Files                 ?     305           
  Lines                 ?   98564           
  Branches              ?    2277           
============================================
  Hits                  ?    1703           
  Misses                ?   96830           
  Partials              ?      31           
```

| [Flag](https://app.codecov.io/gh/microsoft/mu_basecore/pull/1355/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=microsoft) | Coverage Δ | |
|---|---|---|
| [NetworkPkg](https://app.codecov.io/gh/microsoft/mu_basecore/pull/1355/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=microsoft) | `0.55% <ø> (?)` | |
| [UefiCpuPkg](https://app.codecov.io/gh/microsoft/mu_basecore/pull/1355/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=microsoft) | `4.86% <ø> (?)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=microsoft#carryforward-flags-in-the-pull-request-comment) to find out more.
</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/microsoft/mu_basecore/pull/1355?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=microsoft).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=microsoft).
<details><summary> :rocket: New features to boost your workflow: </summary>

- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
</details>
author_association: NONE
user: login: liqiqiii
created_at: 2025-05-13T20:36:03Z
body: Wait for some final validation of the new function.
author_association: CONTRIBUTOR
user: login: liqiqiii
created_at: 2025-05-15T00:10:41Z
body: > Wait for some final validation of the new function.

all set, test passed. Ready for checkin
author_association: CONTRIBUTOR