]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Application/Cryptest/RsaVerify2.c
CryptoPkg: Add new API to retrieve commonName of X.509 certificate
[mirror_edk2.git] / CryptoPkg / Application / Cryptest / RsaVerify2.c
index b6fdfb5c38e1e052ce14eaf063c4004667be1fac..9db43d6eef7aed481a1674b2f24b36d29e6e21d9 100644 (file)
@@ -1,7 +1,7 @@
 /** @file  \r
   Application for RSA Key Retrieving (from PEM and X509) & Signature Validation.\r
 \r
-Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -187,6 +187,11 @@ GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 MsgHash[] = {
   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09\r
   };\r
 \r
+//\r
+// Payload for PKCS#7 Signing & Verification Validation.\r
+//\r
+GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *Payload = "Payload Data for PKCS#7 Signing";\r
+\r
 /**\r
   Validate UEFI-OpenSSL RSA Key Retrieving & Signature Interfaces.\r
 \r
@@ -199,13 +204,17 @@ ValidateCryptRsa2 (
   VOID\r
   )\r
 {\r
-  BOOLEAN  Status;\r
-  VOID     *RsaPrivKey;\r
-  VOID     *RsaPubKey;\r
-  UINT8    *Signature;\r
-  UINTN    SigSize;\r
-  UINT8    *Subject;\r
-  UINTN    SubjectSize;\r
+  BOOLEAN        Status;\r
+  VOID           *RsaPrivKey;\r
+  VOID           *RsaPubKey;\r
+  UINT8          *Signature;\r
+  UINTN          SigSize;\r
+  UINT8          *Subject;\r
+  UINTN          SubjectSize;\r
+  RETURN_STATUS  ReturnStatus;\r
+  CHAR8          CommonName[64];\r
+  CHAR16         CommonNameUnicode[64];\r
+  UINTN          CommonNameSize;\r
 \r
   Print (L"\nUEFI-OpenSSL RSA Key Retrieving Testing: ");\r
 \r
@@ -281,6 +290,20 @@ ValidateCryptRsa2 (
     Print (L"[Pass]");\r
   }\r
 \r
+  //\r
+  // Get CommonName from X509 Certificate Subject\r
+  //\r
+  CommonNameSize = 64;\r
+  ZeroMem (CommonName, CommonNameSize);\r
+  ReturnStatus = X509GetCommonName (TestCert, sizeof (TestCert), CommonName, &CommonNameSize);\r
+  if (RETURN_ERROR (ReturnStatus)) {\r
+    Print (L"\n  - Retrieving Common Name - [Fail]");\r
+    return EFI_ABORTED;\r
+  } else {\r
+    AsciiStrToUnicodeStrS (CommonName, CommonNameUnicode, CommonNameSize);\r
+    Print (L"\n  - Retrieving Common Name = \"%s\" (Size = %d)", CommonNameUnicode, CommonNameSize);\r
+  }\r
+\r
   //\r
   // X509 Certificate Verification.\r
   //\r
@@ -303,3 +326,88 @@ ValidateCryptRsa2 (
 \r
   return EFI_SUCCESS;\r
 }\r
+\r
+/**\r
+  Validate UEFI-OpenSSL PKCS#7 Signing & Verification Interfaces.\r
+\r
+  @retval  EFI_SUCCESS  Validation succeeded.\r
+  @retval  EFI_ABORTED  Validation failed.\r
+\r
+**/\r
+EFI_STATUS\r
+ValidateCryptPkcs7 (\r
+  VOID\r
+  )\r
+{\r
+  BOOLEAN  Status;\r
+  UINT8    *P7SignedData;\r
+  UINTN    P7SignedDataSize;\r
+  UINT8    *SignCert;\r
+\r
+  P7SignedData = NULL;\r
+  SignCert     = NULL;\r
+\r
+  Print (L"\nUEFI-OpenSSL PKCS#7 Signing & Verification Testing: ");\r
+\r
+  Print (L"\n- Create PKCS#7 signedData ...");\r
+\r
+  //\r
+  // Construct Signer Certificate from RAW data.\r
+  //\r
+  Status = X509ConstructCertificate (TestCert, sizeof (TestCert), (UINT8 **) &SignCert);\r
+  if (!Status || SignCert == NULL) {\r
+    Print (L"[Fail]");\r
+    goto _Exit;\r
+  } else {\r
+    Print (L"[Pass]");\r
+  }\r
+\r
+  //\r
+  // Create PKCS#7 signedData on Payload. \r
+  // Note: Caller should release P7SignedData manually.\r
+  //\r
+  Status = Pkcs7Sign (\r
+             TestKeyPem,\r
+             sizeof (TestKeyPem),\r
+             (CONST UINT8 *) PemPass,\r
+             (UINT8 *) Payload,\r
+             AsciiStrLen (Payload),\r
+             SignCert,\r
+             NULL,\r
+             &P7SignedData,\r
+             &P7SignedDataSize\r
+             );\r
+  if (!Status || P7SignedDataSize == 0) {\r
+    Print (L"[Fail]");\r
+    goto _Exit;\r
+  } else {\r
+    Print (L"[Pass]");\r
+  }\r
+\r
+  Print (L"\n- Verify PKCS#7 signedData ...");\r
+\r
+  Status = Pkcs7Verify (\r
+             P7SignedData,\r
+             P7SignedDataSize,\r
+             TestCACert,\r
+             sizeof (TestCACert),\r
+             (UINT8 *) Payload,\r
+             AsciiStrLen (Payload)\r
+             );\r
+  if (!Status) {\r
+    Print (L"[Fail]");\r
+  } else {\r
+    Print (L"[Pass]");\r
+  }\r
+\r
+_Exit:\r
+  if (P7SignedData != NULL) {\r
+    FreePool (P7SignedData);\r
+  }\r
+  if (SignCert != NULL) {\r
+    X509Free (SignCert);\r
+  }\r
+\r
+  Print (L"\n");\r
+  return EFI_SUCCESS;\r
+}\r