![]() |
|
|||
File indexing completed on 2025-08-27 09:37:32
0001 /** 0002 * \file pem.h 0003 * 0004 * \brief Privacy Enhanced Mail (PEM) decoding 0005 */ 0006 /* 0007 * Copyright The Mbed TLS Contributors 0008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 0009 */ 0010 #ifndef MBEDTLS_PEM_H 0011 #define MBEDTLS_PEM_H 0012 #include "mbedtls/private_access.h" 0013 0014 #include "mbedtls/build_info.h" 0015 0016 #include <stddef.h> 0017 0018 /** 0019 * \name PEM Error codes 0020 * These error codes are returned in case of errors reading the 0021 * PEM data. 0022 * \{ 0023 */ 0024 /** No PEM header or footer found. */ 0025 #define MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT -0x1080 0026 /** PEM string is not as expected. */ 0027 #define MBEDTLS_ERR_PEM_INVALID_DATA -0x1100 0028 /** Failed to allocate memory. */ 0029 #define MBEDTLS_ERR_PEM_ALLOC_FAILED -0x1180 0030 /** RSA IV is not in hex-format. */ 0031 #define MBEDTLS_ERR_PEM_INVALID_ENC_IV -0x1200 0032 /** Unsupported key encryption algorithm. */ 0033 #define MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG -0x1280 0034 /** Private key password can't be empty. */ 0035 #define MBEDTLS_ERR_PEM_PASSWORD_REQUIRED -0x1300 0036 /** Given private key password does not allow for correct decryption. */ 0037 #define MBEDTLS_ERR_PEM_PASSWORD_MISMATCH -0x1380 0038 /** Unavailable feature, e.g. hashing/encryption combination. */ 0039 #define MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE -0x1400 0040 /** Bad input parameters to function. */ 0041 #define MBEDTLS_ERR_PEM_BAD_INPUT_DATA -0x1480 0042 /** \} name PEM Error codes */ 0043 0044 #ifdef __cplusplus 0045 extern "C" { 0046 #endif 0047 0048 #if defined(MBEDTLS_PEM_PARSE_C) 0049 /** 0050 * \brief PEM context structure 0051 */ 0052 typedef struct mbedtls_pem_context { 0053 unsigned char *MBEDTLS_PRIVATE(buf); /*!< buffer for decoded data */ 0054 size_t MBEDTLS_PRIVATE(buflen); /*!< length of the buffer */ 0055 unsigned char *MBEDTLS_PRIVATE(info); /*!< buffer for extra header information */ 0056 } 0057 mbedtls_pem_context; 0058 0059 /** 0060 * \brief PEM context setup 0061 * 0062 * \param ctx context to be initialized 0063 */ 0064 void mbedtls_pem_init(mbedtls_pem_context *ctx); 0065 0066 /** 0067 * \brief Read a buffer for PEM information and store the resulting 0068 * data into the specified context buffers. 0069 * 0070 * \param ctx context to use 0071 * \param header header string to seek and expect 0072 * \param footer footer string to seek and expect 0073 * \param data source data to look in (must be nul-terminated) 0074 * \param pwd password for decryption (can be NULL) 0075 * \param pwdlen length of password 0076 * \param use_len destination for total length used from data buffer. It is 0077 * set after header is correctly read, so unless you get 0078 * MBEDTLS_ERR_PEM_BAD_INPUT_DATA or 0079 * MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT, use_len is 0080 * the length to skip. 0081 * 0082 * \note Attempts to check password correctness by verifying if 0083 * the decrypted text starts with an ASN.1 sequence of 0084 * appropriate length 0085 * 0086 * \note \c mbedtls_pem_free must be called on PEM context before 0087 * the PEM context can be reused in another call to 0088 * \c mbedtls_pem_read_buffer 0089 * 0090 * \return 0 on success, or a specific PEM error code 0091 */ 0092 int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer, 0093 const unsigned char *data, 0094 const unsigned char *pwd, 0095 size_t pwdlen, size_t *use_len); 0096 0097 /** 0098 * \brief Get the pointer to the decoded binary data in a PEM context. 0099 * 0100 * \param ctx PEM context to access. 0101 * \param buflen On success, this will contain the length of the binary data. 0102 * This must be a valid (non-null) pointer. 0103 * 0104 * \return A pointer to the decoded binary data. 0105 * 0106 * \note The returned pointer remains valid only until \p ctx is 0107 modified or freed. 0108 */ 0109 static inline const unsigned char *mbedtls_pem_get_buffer(mbedtls_pem_context *ctx, size_t *buflen) 0110 { 0111 *buflen = ctx->MBEDTLS_PRIVATE(buflen); 0112 return ctx->MBEDTLS_PRIVATE(buf); 0113 } 0114 0115 0116 /** 0117 * \brief PEM context memory freeing 0118 * 0119 * \param ctx context to be freed 0120 */ 0121 void mbedtls_pem_free(mbedtls_pem_context *ctx); 0122 #endif /* MBEDTLS_PEM_PARSE_C */ 0123 0124 #if defined(MBEDTLS_PEM_WRITE_C) 0125 /** 0126 * \brief Write a buffer of PEM information from a DER encoded 0127 * buffer. 0128 * 0129 * \param header The header string to write. 0130 * \param footer The footer string to write. 0131 * \param der_data The DER data to encode. 0132 * \param der_len The length of the DER data \p der_data in Bytes. 0133 * \param buf The buffer to write to. 0134 * \param buf_len The length of the output buffer \p buf in Bytes. 0135 * \param olen The address at which to store the total length written 0136 * or required (if \p buf_len is not enough). 0137 * 0138 * \note You may pass \c NULL for \p buf and \c 0 for \p buf_len 0139 * to request the length of the resulting PEM buffer in 0140 * `*olen`. 0141 * 0142 * \note This function may be called with overlapping \p der_data 0143 * and \p buf buffers. 0144 * 0145 * \return \c 0 on success. 0146 * \return #MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL if \p buf isn't large 0147 * enough to hold the PEM buffer. In this case, `*olen` holds 0148 * the required minimum size of \p buf. 0149 * \return Another PEM or BASE64 error code on other kinds of failure. 0150 */ 0151 int mbedtls_pem_write_buffer(const char *header, const char *footer, 0152 const unsigned char *der_data, size_t der_len, 0153 unsigned char *buf, size_t buf_len, size_t *olen); 0154 #endif /* MBEDTLS_PEM_WRITE_C */ 0155 0156 #ifdef __cplusplus 0157 } 0158 #endif 0159 0160 #endif /* pem.h */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |