1 /**
2  * \file pem.h
3  *
4  * \brief Privacy Enhanced Mail (PEM) decoding
5  */
6 /*
7  *  Copyright The Mbed TLS Contributors
8  *  SPDX-License-Identifier: Apache-2.0
9  *
10  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
11  *  not use this file except in compliance with the License.
12  *  You may obtain a copy of the License at
13  *
14  *  http://www.apache.org/licenses/LICENSE-2.0
15  *
16  *  Unless required by applicable law or agreed to in writing, software
17  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  *  See the License for the specific language governing permissions and
20  *  limitations under the License.
21  */
22 #ifndef MBEDTLS_PEM_H
23 #define MBEDTLS_PEM_H
24 #include "mbedtls/private_access.h"
25 
26 #include "mbedtls/build_info.h"
27 
28 #include <stddef.h>
29 
30 /**
31  * \name PEM Error codes
32  * These error codes are returned in case of errors reading the
33  * PEM data.
34  * \{
35  */
36 /** No PEM header or footer found. */
37 #define MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT          -0x1080
38 /** PEM string is not as expected. */
39 #define MBEDTLS_ERR_PEM_INVALID_DATA                      -0x1100
40 /** Failed to allocate memory. */
41 #define MBEDTLS_ERR_PEM_ALLOC_FAILED                      -0x1180
42 /** RSA IV is not in hex-format. */
43 #define MBEDTLS_ERR_PEM_INVALID_ENC_IV                    -0x1200
44 /** Unsupported key encryption algorithm. */
45 #define MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG                   -0x1280
46 /** Private key password can't be empty. */
47 #define MBEDTLS_ERR_PEM_PASSWORD_REQUIRED                 -0x1300
48 /** Given private key password does not allow for correct decryption. */
49 #define MBEDTLS_ERR_PEM_PASSWORD_MISMATCH                 -0x1380
50 /** Unavailable feature, e.g. hashing/encryption combination. */
51 #define MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE               -0x1400
52 /** Bad input parameters to function. */
53 #define MBEDTLS_ERR_PEM_BAD_INPUT_DATA                    -0x1480
54 /** \} name PEM Error codes */
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 #if defined(MBEDTLS_PEM_PARSE_C)
61 /**
62  * \brief       PEM context structure
63  */
64 typedef struct mbedtls_pem_context
65 {
66     unsigned char *MBEDTLS_PRIVATE(buf);     /*!< buffer for decoded data             */
67     size_t MBEDTLS_PRIVATE(buflen);          /*!< length of the buffer                */
68     unsigned char *MBEDTLS_PRIVATE(info);    /*!< buffer for extra header information */
69 }
70 mbedtls_pem_context;
71 
72 /**
73  * \brief       PEM context setup
74  *
75  * \param ctx   context to be initialized
76  */
77 void mbedtls_pem_init( mbedtls_pem_context *ctx );
78 
79 /**
80  * \brief       Read a buffer for PEM information and store the resulting
81  *              data into the specified context buffers.
82  *
83  * \param ctx       context to use
84  * \param header    header string to seek and expect
85  * \param footer    footer string to seek and expect
86  * \param data      source data to look in (must be nul-terminated)
87  * \param pwd       password for decryption (can be NULL)
88  * \param pwdlen    length of password
89  * \param use_len   destination for total length used (set after header is
90  *                  correctly read, so unless you get
91  *                  MBEDTLS_ERR_PEM_BAD_INPUT_DATA or
92  *                  MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT, use_len is
93  *                  the length to skip)
94  *
95  * \note            Attempts to check password correctness by verifying if
96  *                  the decrypted text starts with an ASN.1 sequence of
97  *                  appropriate length
98  *
99  * \note            \c mbedtls_pem_free must be called on PEM context before
100  *                  the PEM context can be reused in another call to
101  *                  \c mbedtls_pem_read_buffer
102  *
103  * \return          0 on success, or a specific PEM error code
104  */
105 int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
106                      const unsigned char *data,
107                      const unsigned char *pwd,
108                      size_t pwdlen, size_t *use_len );
109 
110 /**
111  * \brief       Get the pointer to the decoded binary data in a PEM context.
112  *
113  * \param ctx       PEM context to access.
114  * \param buflen    On success, this will contain the length of the binary data.
115  *                  This must be a valid (non-null) pointer.
116  *
117  * \return          A pointer to the decoded binary data.
118  *
119  * \note            The returned pointer remains valid only until \p ctx is
120                     modified or freed.
121  */
mbedtls_pem_get_buffer(mbedtls_pem_context * ctx,size_t * buflen)122 static inline const unsigned char *mbedtls_pem_get_buffer( mbedtls_pem_context *ctx, size_t *buflen )
123 {
124     *buflen = ctx->MBEDTLS_PRIVATE(buflen);
125     return( ctx->MBEDTLS_PRIVATE(buf) );
126 }
127 
128 
129 /**
130  * \brief       PEM context memory freeing
131  *
132  * \param ctx   context to be freed
133  */
134 void mbedtls_pem_free( mbedtls_pem_context *ctx );
135 #endif /* MBEDTLS_PEM_PARSE_C */
136 
137 #if defined(MBEDTLS_PEM_WRITE_C)
138 /**
139  * \brief           Write a buffer of PEM information from a DER encoded
140  *                  buffer.
141  *
142  * \param header    The header string to write.
143  * \param footer    The footer string to write.
144  * \param der_data  The DER data to encode.
145  * \param der_len   The length of the DER data \p der_data in Bytes.
146  * \param buf       The buffer to write to.
147  * \param buf_len   The length of the output buffer \p buf in Bytes.
148  * \param olen      The address at which to store the total length written
149  *                  or required (if \p buf_len is not enough).
150  *
151  * \note            You may pass \c NULL for \p buf and \c 0 for \p buf_len
152  *                  to request the length of the resulting PEM buffer in
153  *                  `*olen`.
154  *
155  * \note            This function may be called with overlapping \p der_data
156  *                  and \p buf buffers.
157  *
158  * \return          \c 0 on success.
159  * \return          #MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL if \p buf isn't large
160  *                  enough to hold the PEM buffer. In  this case, `*olen` holds
161  *                  the required minimum size of \p buf.
162  * \return          Another PEM or BASE64 error code on other kinds of failure.
163  */
164 int mbedtls_pem_write_buffer( const char *header, const char *footer,
165                       const unsigned char *der_data, size_t der_len,
166                       unsigned char *buf, size_t buf_len, size_t *olen );
167 #endif /* MBEDTLS_PEM_WRITE_C */
168 
169 #ifdef __cplusplus
170 }
171 #endif
172 
173 #endif /* pem.h */
174