CryptPem.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /** @file
  2. PEM (Privacy Enhanced Mail) Format Handler Wrapper Implementation over OpenSSL.
  3. Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "InternalCryptLib.h"
  7. #include <openssl/pem.h>
  8. /**
  9. Callback function for password phrase conversion used for retrieving the encrypted PEM.
  10. @param[out] Buf Pointer to the buffer to write the passphrase to.
  11. @param[in] Size Maximum length of the passphrase (i.e. the size of Buf).
  12. @param[in] Flag A flag which is set to 0 when reading and 1 when writing.
  13. @param[in] Key Key data to be passed to the callback routine.
  14. @retval The number of characters in the passphrase or 0 if an error occurred.
  15. **/
  16. INTN
  17. PasswordCallback (
  18. OUT CHAR8 *Buf,
  19. IN INTN Size,
  20. IN INTN Flag,
  21. IN VOID *Key
  22. )
  23. {
  24. INTN KeyLength;
  25. ZeroMem ((VOID *) Buf, (UINTN) Size);
  26. if (Key != NULL) {
  27. //
  28. // Duplicate key phrase directly.
  29. //
  30. KeyLength = (INTN) AsciiStrLen ((CHAR8 *)Key);
  31. KeyLength = (KeyLength > Size ) ? Size : KeyLength;
  32. CopyMem (Buf, Key, (UINTN) KeyLength);
  33. return KeyLength;
  34. } else {
  35. return 0;
  36. }
  37. }
  38. /**
  39. Retrieve the RSA Private Key from the password-protected PEM key data.
  40. @param[in] PemData Pointer to the PEM-encoded key data to be retrieved.
  41. @param[in] PemSize Size of the PEM key data in bytes.
  42. @param[in] Password NULL-terminated passphrase used for encrypted PEM key data.
  43. @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
  44. RSA private key component. Use RsaFree() function to free the
  45. resource.
  46. If PemData is NULL, then return FALSE.
  47. If RsaContext is NULL, then return FALSE.
  48. @retval TRUE RSA Private Key was retrieved successfully.
  49. @retval FALSE Invalid PEM key data or incorrect password.
  50. **/
  51. BOOLEAN
  52. EFIAPI
  53. RsaGetPrivateKeyFromPem (
  54. IN CONST UINT8 *PemData,
  55. IN UINTN PemSize,
  56. IN CONST CHAR8 *Password,
  57. OUT VOID **RsaContext
  58. )
  59. {
  60. BOOLEAN Status;
  61. BIO *PemBio;
  62. //
  63. // Check input parameters.
  64. //
  65. if (PemData == NULL || RsaContext == NULL || PemSize > INT_MAX) {
  66. return FALSE;
  67. }
  68. //
  69. // Add possible block-cipher descriptor for PEM data decryption.
  70. // NOTE: Only support most popular ciphers (3DES, AES) for the encrypted PEM.
  71. //
  72. if (EVP_add_cipher (EVP_des_ede3_cbc ()) == 0) {
  73. return FALSE;
  74. }
  75. if (EVP_add_cipher (EVP_aes_128_cbc ()) == 0) {
  76. return FALSE;
  77. }
  78. if (EVP_add_cipher (EVP_aes_192_cbc ()) == 0) {
  79. return FALSE;
  80. }
  81. if (EVP_add_cipher (EVP_aes_256_cbc ()) == 0) {
  82. return FALSE;
  83. }
  84. Status = FALSE;
  85. //
  86. // Read encrypted PEM Data.
  87. //
  88. PemBio = BIO_new (BIO_s_mem ());
  89. if (PemBio == NULL) {
  90. goto _Exit;
  91. }
  92. if (BIO_write (PemBio, PemData, (int) PemSize) <= 0) {
  93. goto _Exit;
  94. }
  95. //
  96. // Retrieve RSA Private Key from encrypted PEM data.
  97. //
  98. *RsaContext = PEM_read_bio_RSAPrivateKey (PemBio, NULL, (pem_password_cb *) &PasswordCallback, (void *) Password);
  99. if (*RsaContext != NULL) {
  100. Status = TRUE;
  101. }
  102. _Exit:
  103. //
  104. // Release Resources.
  105. //
  106. BIO_free (PemBio);
  107. return Status;
  108. }