/* Function to get the common name from the X509 format certificate. The buffer is callee allocated and should be freed by the caller. */
EFI_STATUS GetCommonNameFromX509(IN EFI_SIGNATURE_LIST *ListEntry, IN EFI_SIGNATURE_DATA *DataEntry, OUT CHAR16 **BufferToReturn)
/* Function to get the common name from the X509 format certificate. The buffer is callee allocated and should be freed by the caller. */
EFI_STATUS GetCommonNameFromX509(IN EFI_SIGNATURE_LIST *ListEntry, IN EFI_SIGNATURE_DATA *DataEntry, OUT CHAR16 **BufferToReturn)
{
  EFI_STATUS  Status;
  CHAR8       *CNBuffer;
  UINTN       CNBufferSize;
  Status   = EFI_SUCCESS;
  CNBuffer = NULL;
  CNBuffer = AllocateZeroPool (256);
  if (CNBuffer == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    goto ON_EXIT;
  }
  CNBufferSize = 256;
  X509GetCommonName (
    (UINT8 *)DataEntry + sizeof (EFI_GUID),
    ListEntry->SignatureSize - sizeof (EFI_GUID),
    CNBuffer,
    &CNBufferSize
    );
  *BufferToReturn = AllocateZeroPool (256 * sizeof (CHAR16));
  if (*BufferToReturn == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    goto ON_EXIT;
  }
  AsciiStrToUnicodeStrS (CNBuffer, *BufferToReturn, 256);
ON_EXIT:
  SECUREBOOT_FREE_NON_NULL (CNBuffer);
  return Status;
}