]>
Commit | Line | Data |
---|---|---|
1 | /** | |
2 | * \file pem.h | |
3 | * | |
4 | * \brief Privacy Enhanced Mail (PEM) decoding | |
5 | */ | |
6 | /* | |
7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | |
8 | * SPDX-License-Identifier: GPL-2.0 | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License along | |
21 | * with this program; if not, write to the Free Software Foundation, Inc., | |
22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
23 | * | |
24 | * This file is part of mbed TLS (https://tls.mbed.org) | |
25 | */ | |
26 | #ifndef MBEDTLS_PEM_H | |
27 | #define MBEDTLS_PEM_H | |
28 | ||
29 | #include <stddef.h> | |
30 | ||
31 | /** | |
32 | * \name PEM Error codes | |
33 | * These error codes are returned in case of errors reading the | |
34 | * PEM data. | |
35 | * \{ | |
36 | */ | |
37 | #define MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT -0x1080 /**< No PEM header or footer found. */ | |
38 | #define MBEDTLS_ERR_PEM_INVALID_DATA -0x1100 /**< PEM string is not as expected. */ | |
39 | #define MBEDTLS_ERR_PEM_ALLOC_FAILED -0x1180 /**< Failed to allocate memory. */ | |
40 | #define MBEDTLS_ERR_PEM_INVALID_ENC_IV -0x1200 /**< RSA IV is not in hex-format. */ | |
41 | #define MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG -0x1280 /**< Unsupported key encryption algorithm. */ | |
42 | #define MBEDTLS_ERR_PEM_PASSWORD_REQUIRED -0x1300 /**< Private key password can't be empty. */ | |
43 | #define MBEDTLS_ERR_PEM_PASSWORD_MISMATCH -0x1380 /**< Given private key password does not allow for correct decryption. */ | |
44 | #define MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE -0x1400 /**< Unavailable feature, e.g. hashing/encryption combination. */ | |
45 | #define MBEDTLS_ERR_PEM_BAD_INPUT_DATA -0x1480 /**< Bad input parameters to function. */ | |
46 | /* \} name */ | |
47 | ||
48 | #ifdef __cplusplus | |
49 | extern "C" { | |
50 | #endif | |
51 | ||
52 | #if defined(MBEDTLS_PEM_PARSE_C) | |
53 | /** | |
54 | * \brief PEM context structure | |
55 | */ | |
56 | typedef struct mbedtls_pem_context | |
57 | { | |
58 | unsigned char *buf; /*!< buffer for decoded data */ | |
59 | size_t buflen; /*!< length of the buffer */ | |
60 | unsigned char *info; /*!< buffer for extra header information */ | |
61 | } | |
62 | mbedtls_pem_context; | |
63 | ||
64 | /** | |
65 | * \brief PEM context setup | |
66 | * | |
67 | * \param ctx context to be initialized | |
68 | */ | |
69 | void mbedtls_pem_init( mbedtls_pem_context *ctx ); | |
70 | ||
71 | /** | |
72 | * \brief Read a buffer for PEM information and store the resulting | |
73 | * data into the specified context buffers. | |
74 | * | |
75 | * \param ctx context to use | |
76 | * \param header header string to seek and expect | |
77 | * \param footer footer string to seek and expect | |
78 | * \param data source data to look in (must be nul-terminated) | |
79 | * \param pwd password for decryption (can be NULL) | |
80 | * \param pwdlen length of password | |
81 | * \param use_len destination for total length used (set after header is | |
82 | * correctly read, so unless you get | |
83 | * MBEDTLS_ERR_PEM_BAD_INPUT_DATA or | |
84 | * MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT, use_len is | |
85 | * the length to skip) | |
86 | * | |
87 | * \note Attempts to check password correctness by verifying if | |
88 | * the decrypted text starts with an ASN.1 sequence of | |
89 | * appropriate length | |
90 | * | |
91 | * \return 0 on success, or a specific PEM error code | |
92 | */ | |
93 | int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer, | |
94 | const unsigned char *data, | |
95 | const unsigned char *pwd, | |
96 | size_t pwdlen, size_t *use_len ); | |
97 | ||
98 | /** | |
99 | * \brief PEM context memory freeing | |
100 | * | |
101 | * \param ctx context to be freed | |
102 | */ | |
103 | void mbedtls_pem_free( mbedtls_pem_context *ctx ); | |
104 | #endif /* MBEDTLS_PEM_PARSE_C */ | |
105 | ||
106 | #if defined(MBEDTLS_PEM_WRITE_C) | |
107 | /** | |
108 | * \brief Write a buffer of PEM information from a DER encoded | |
109 | * buffer. | |
110 | * | |
111 | * \param header header string to write | |
112 | * \param footer footer string to write | |
113 | * \param der_data DER data to write | |
114 | * \param der_len length of the DER data | |
115 | * \param buf buffer to write to | |
116 | * \param buf_len length of output buffer | |
117 | * \param olen total length written / required (if buf_len is not enough) | |
118 | * | |
119 | * \return 0 on success, or a specific PEM or BASE64 error code. On | |
120 | * MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL olen is the required | |
121 | * size. | |
122 | */ | |
123 | int mbedtls_pem_write_buffer( const char *header, const char *footer, | |
124 | const unsigned char *der_data, size_t der_len, | |
125 | unsigned char *buf, size_t buf_len, size_t *olen ); | |
126 | #endif /* MBEDTLS_PEM_WRITE_C */ | |
127 | ||
128 | #ifdef __cplusplus | |
129 | } | |
130 | #endif | |
131 | ||
132 | #endif /* pem.h */ |