TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptPem.c
Go to the documentation of this file.
1
9#include "InternalCryptLib.h"
10#include <mbedtls/pem.h>
11#include <mbedtls/pk.h>
12#include <mbedtls/rsa.h>
13#include <mbedtls/ecp.h>
14#include <mbedtls/ecdh.h>
15#include <mbedtls/ecdsa.h>
16
34BOOLEAN
35EFIAPI
37 IN CONST UINT8 *PemData,
38 IN UINTN PemSize,
39 IN CONST CHAR8 *Password,
40 OUT VOID **RsaContext
41 )
42{
43 INT32 Ret;
44 mbedtls_pk_context Pk;
45 mbedtls_rsa_context *Rsa;
46 UINT8 *NewPemData;
47 UINTN PasswordLen;
48
49 if ((PemData == NULL) || (RsaContext == NULL) || (PemSize > INT_MAX)) {
50 return FALSE;
51 }
52
53 NewPemData = NULL;
54 if (PemData[PemSize - 1] != 0) {
55 NewPemData = AllocateZeroPool (PemSize + 1);
56 if (NewPemData == NULL) {
57 return FALSE;
58 }
59
60 CopyMem (NewPemData, PemData, PemSize + 1);
61 NewPemData[PemSize] = 0;
62 PemData = NewPemData;
63 PemSize += 1;
64 }
65
66 mbedtls_pk_init (&Pk);
67
68 if (Password != NULL) {
69 PasswordLen = AsciiStrLen (Password);
70 } else {
71 PasswordLen = 0;
72 }
73
74 Ret = mbedtls_pk_parse_key (&Pk, PemData, PemSize, (CONST UINT8 *)Password, PasswordLen, NULL, NULL);
75
76 if (NewPemData != NULL) {
77 FreePool (NewPemData);
78 NewPemData = NULL;
79 }
80
81 if (Ret != 0) {
82 mbedtls_pk_free (&Pk);
83 return FALSE;
84 }
85
86 if (mbedtls_pk_get_type (&Pk) != MBEDTLS_PK_RSA) {
87 mbedtls_pk_free (&Pk);
88 return FALSE;
89 }
90
91 Rsa = RsaNew ();
92 if (Rsa == NULL) {
93 mbedtls_pk_free (&Pk);
94 return FALSE;
95 }
96
97 Ret = mbedtls_rsa_copy (Rsa, mbedtls_pk_rsa (Pk));
98 if (Ret != 0) {
99 RsaFree (Rsa);
100 mbedtls_pk_free (&Pk);
101 return FALSE;
102 }
103
104 mbedtls_pk_free (&Pk);
105
106 *RsaContext = Rsa;
107 return TRUE;
108}
109
127BOOLEAN
128EFIAPI
130 IN CONST UINT8 *PemData,
131 IN UINTN PemSize,
132 IN CONST CHAR8 *Password,
133 OUT VOID **EcContext
134 )
135{
136 ASSERT (FALSE);
137 return FALSE;
138}
UINT64 UINTN
VOID *EFIAPI RsaNew(VOID)
Definition: CryptRsaBasic.c:30
VOID EFIAPI RsaFree(IN VOID *RsaContext)
Definition: CryptRsaBasic.c:48
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
Definition: String.c:641
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
BOOLEAN EFIAPI RsaGetPrivateKeyFromPem(IN CONST UINT8 *PemData, IN UINTN PemSize, IN CONST CHAR8 *Password, OUT VOID **RsaContext)
Definition: CryptPem.c:66
BOOLEAN EFIAPI EcGetPrivateKeyFromPem(IN CONST UINT8 *PemData, IN UINTN PemSize, IN CONST CHAR8 *Password, OUT VOID **EcContext)
Definition: CryptPem.c:149