blob: a0a03b061cb93e477f94683dac8f9fb5d778b5c8 [file] [log] [blame]
Paul Bakker96743fc2011-02-12 14:30:57 +00001/**
2 * \file pem.h
3 *
4 * \brief Privacy Enhanced Mail (PEM) decoding
5 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker96743fc2011-02-12 14:30:57 +00007 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker96743fc2011-02-12 14:30:57 +00009 *
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#ifndef POLARSSL_PEM_H
25#define POLARSSL_PEM_H
26
Paul Bakker23986e52011-04-24 08:57:21 +000027#include <string.h>
28
Paul Bakker96743fc2011-02-12 14:30:57 +000029/**
30 * \name PEM Error codes
31 * These error codes are returned in case of errors reading the
32 * PEM data.
33 * \{
34 */
Paul Bakker9255e832013-06-06 14:58:28 +020035#define POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT -0x1080 /**< No PEM header or footer found. */
Paul Bakker9d781402011-05-09 16:17:09 +000036#define POLARSSL_ERR_PEM_INVALID_DATA -0x1100 /**< PEM string is not as expected. */
37#define POLARSSL_ERR_PEM_MALLOC_FAILED -0x1180 /**< Failed to allocate memory. */
38#define POLARSSL_ERR_PEM_INVALID_ENC_IV -0x1200 /**< RSA IV is not in hex-format. */
39#define POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG -0x1280 /**< Unsupported key encryption algorithm. */
40#define POLARSSL_ERR_PEM_PASSWORD_REQUIRED -0x1300 /**< Private key password can't be empty. */
41#define POLARSSL_ERR_PEM_PASSWORD_MISMATCH -0x1380 /**< Given private key password does not allow for correct decryption. */
42#define POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE -0x1400 /**< Unavailable feature, e.g. hashing/encryption combination. */
Paul Bakker9255e832013-06-06 14:58:28 +020043#define POLARSSL_ERR_PEM_BAD_INPUT_DATA -0x1480 /**< Bad input parameters to function. */
Paul Bakker96743fc2011-02-12 14:30:57 +000044/* \} name */
45
46/**
47 * \brief PEM context structure
48 */
49typedef struct
50{
51 unsigned char *buf; /*!< buffer for decoded data */
Paul Bakker23986e52011-04-24 08:57:21 +000052 size_t buflen; /*!< length of the buffer */
Paul Bakker96743fc2011-02-12 14:30:57 +000053 unsigned char *info; /*!< buffer for extra header information */
54}
55pem_context;
56
57#ifdef __cplusplus
58extern "C" {
59#endif
60
61/**
62 * \brief PEM context setup
63 *
64 * \param ctx context to be initialized
65 */
66void pem_init( pem_context *ctx );
67
68/**
69 * \brief Read a buffer for PEM information and store the resulting
70 * data into the specified context buffers.
71 *
72 * \param ctx context to use
73 * \param header header string to seek and expect
74 * \param footer footer string to seek and expect
75 * \param data source data to look in
76 * \param pwd password for decryption (can be NULL)
77 * \param pwdlen length of password
Paul Bakker9255e832013-06-06 14:58:28 +020078 * \param use_len destination for total length used (set after header is
79 * correctly read, so unless you get
80 * POLARSSL_ERR_PEM_BAD_INPUT_DATA or
81 * POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT, use_len is
82 * the length to skip)
Paul Bakker96743fc2011-02-12 14:30:57 +000083 *
84 * \return 0 on success, ior a specific PEM error code
85 */
86int pem_read_buffer( pem_context *ctx, char *header, char *footer,
87 const unsigned char *data,
88 const unsigned char *pwd,
Paul Bakker23986e52011-04-24 08:57:21 +000089 size_t pwdlen, size_t *use_len );
Paul Bakker96743fc2011-02-12 14:30:57 +000090
91/**
92 * \brief PEM context memory freeing
93 *
94 * \param ctx context to be freed
95 */
96void pem_free( pem_context *ctx );
97
98#ifdef __cplusplus
99}
100#endif
101
102#endif /* pem.h */