Privacy Enhanced Mail (PEM) decoding.
More...
#include "mbedtls/private_access.h"
#include "tf-psa-crypto/build_info.h"
#include "mbedtls/compat-3-crypto.h"
#include <stddef.h>
Go to the source code of this file.
|
| void | mbedtls_pem_init (mbedtls_pem_context *ctx) |
| | PEM context setup.
|
| int | mbedtls_pem_read_buffer (mbedtls_pem_context *ctx, const char *header, const char *footer, const unsigned char *data, const unsigned char *pwd, size_t pwdlen, size_t *use_len) |
| | Read a buffer for PEM information and store the resulting data into the specified context buffers.
|
| static const unsigned char * | mbedtls_pem_get_buffer (mbedtls_pem_context *ctx, size_t *buflen) |
| | Get the pointer to the decoded binary data in a PEM context.
|
| void | mbedtls_pem_free (mbedtls_pem_context *ctx) |
| | PEM context memory freeing.
|
| int | mbedtls_pem_write_buffer (const char *header, const char *footer, const unsigned char *der_data, size_t der_len, unsigned char *buf, size_t buf_len, size_t *olen) |
| | Write a buffer of PEM information from a DER encoded buffer.
|
Privacy Enhanced Mail (PEM) decoding.
Definition in file pem.h.
◆ MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE
| #define MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE -0x1400 |
Unavailable feature, e.g. hashing/encryption combination.
Definition at line 38 of file pem.h.
◆ MBEDTLS_ERR_PEM_INVALID_DATA
| #define MBEDTLS_ERR_PEM_INVALID_DATA -0x1100 |
PEM string is not as expected.
Definition at line 28 of file pem.h.
◆ MBEDTLS_ERR_PEM_INVALID_ENC_IV
| #define MBEDTLS_ERR_PEM_INVALID_ENC_IV -0x1200 |
RSA IV is not in hex-format.
Definition at line 30 of file pem.h.
◆ MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT
| #define MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT -0x1080 |
No PEM header or footer found.
Definition at line 26 of file pem.h.
◆ MBEDTLS_ERR_PEM_PASSWORD_MISMATCH
| #define MBEDTLS_ERR_PEM_PASSWORD_MISMATCH -0x1380 |
Given private key password does not allow for correct decryption.
Definition at line 36 of file pem.h.
◆ MBEDTLS_ERR_PEM_PASSWORD_REQUIRED
| #define MBEDTLS_ERR_PEM_PASSWORD_REQUIRED -0x1300 |
Private key password can't be empty.
Definition at line 34 of file pem.h.
◆ MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG
| #define MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG -0x1280 |
Unsupported key encryption algorithm.
Definition at line 32 of file pem.h.
◆ mbedtls_pem_context
| typedef struct mbedtls_pem_context mbedtls_pem_context |
◆ mbedtls_pem_free()
PEM context memory freeing.
- Parameters
-
◆ mbedtls_pem_get_buffer()
Get the pointer to the decoded binary data in a PEM context.
- Parameters
-
| ctx | PEM context to access. |
| buflen | On success, this will contain the length of the binary data. This must be a valid (non-null) pointer. |
- Returns
- A pointer to the decoded binary data.
- Note
- The returned pointer remains valid only until
ctx is modified or freed.
Definition at line 130 of file pem.h.
◆ mbedtls_pem_init()
PEM context setup.
- Parameters
-
| ctx | context to be initialized |
◆ mbedtls_pem_read_buffer()
| int mbedtls_pem_read_buffer |
( |
mbedtls_pem_context * | ctx, |
|
|
const char * | header, |
|
|
const char * | footer, |
|
|
const unsigned char * | data, |
|
|
const unsigned char * | pwd, |
|
|
size_t | pwdlen, |
|
|
size_t * | use_len ) |
Read a buffer for PEM information and store the resulting data into the specified context buffers.
- Warning
- PEM decryption is inherently prone to oracle attacks. If an adversary can submit arbitrary ciphertexts and can observe whether they are accepted or rejected, the adversary may be able to decrypt ciphertexts without knowing the password. Furthermore, this function is not constant-time, which improves the adversary's efficiency. (Note also that subsequent processing after decryption is likely to improve the adversary's efficiency as well.) Therefore, PEM decryption should only be used in environments where the ciphertext comes from a trusted source (for example, a storage medium that may be stolen but is not at risk of silent modifications by an attacker).
-
PEM encryption uses an obsolete password protection mechanism that makes brute force attack fast. Therefore, the password used for encryption should be a sufficiently long, randomly generated string.
- Note
- To encrypt private keys, we recommend PKCS8 encryption, which is supported by TF-PSA-Crypto's PK module, is a significant improvement on the weaknesses of PEM encryption, and works equally well with PEM and binary (DER) encoding.
- Deprecated
- Encrypted PEM (with a non-null password) is deprecated, and should only be used to access legacy data in environemnts where it is safe.
- Parameters
-
| ctx | context to use |
| header | header string to seek and expect |
| footer | footer string to seek and expect |
| data | source data to look in (must be nul-terminated) |
| pwd | password for decryption (can be NULL) |
| pwdlen | length of password |
| use_len | destination for total length used from data buffer. It is set after header is correctly read, so unless you get PSA_ERROR_INVALID_ARGUMENT or MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT, use_len is the length to skip. |
- Note
mbedtls_pem_free must be called on PEM context before the PEM context can be reused in another call to mbedtls_pem_read_buffer
- Returns
- 0 on success, or a specific PEM error code
◆ mbedtls_pem_write_buffer()
| int mbedtls_pem_write_buffer |
( |
const char * | header, |
|
|
const char * | footer, |
|
|
const unsigned char * | der_data, |
|
|
size_t | der_len, |
|
|
unsigned char * | buf, |
|
|
size_t | buf_len, |
|
|
size_t * | olen ) |
Write a buffer of PEM information from a DER encoded buffer.
- Parameters
-
| header | The header string to write. |
| footer | The footer string to write. |
| der_data | The DER data to encode. |
| der_len | The length of the DER data der_data in Bytes. |
| buf | The buffer to write to. |
| buf_len | The length of the output buffer buf in Bytes. |
| olen | The address at which to store the total length written or required (if buf_len is not enough). |
- Note
- You may pass
NULL for buf and 0 for buf_len to request the length of the resulting PEM buffer in *olen.
-
This function may be called with overlapping
der_data and buf buffers.
- Returns
0 on success.
-
PSA_ERROR_BUFFER_TOO_SMALL if
buf isn't large enough to hold the PEM buffer. In this case, *olen holds the required minimum size of buf.
-
Another PEM or BASE64 error code on other kinds of failure.