Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file pem.h |
| 3 | * |
| 4 | * \brief Privacy Enhanced Mail (PEM) decoding |
| 5 | * |
Paul Bakker | 00b2860 | 2013-06-24 13:02:41 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_PEM_H |
| 28 | #define POLARSSL_PEM_H |
| 29 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 32 | /** |
| 33 | * \name PEM Error codes |
| 34 | * These error codes are returned in case of errors reading the |
| 35 | * PEM data. |
| 36 | * \{ |
| 37 | */ |
Paul Bakker | 00b2860 | 2013-06-24 13:02:41 +0200 | [diff] [blame] | 38 | #define POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT -0x1080 /**< No PEM header or footer found. */ |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 39 | #define POLARSSL_ERR_PEM_INVALID_DATA -0x1100 /**< PEM string is not as expected. */ |
| 40 | #define POLARSSL_ERR_PEM_MALLOC_FAILED -0x1180 /**< Failed to allocate memory. */ |
| 41 | #define POLARSSL_ERR_PEM_INVALID_ENC_IV -0x1200 /**< RSA IV is not in hex-format. */ |
| 42 | #define POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG -0x1280 /**< Unsupported key encryption algorithm. */ |
| 43 | #define POLARSSL_ERR_PEM_PASSWORD_REQUIRED -0x1300 /**< Private key password can't be empty. */ |
| 44 | #define POLARSSL_ERR_PEM_PASSWORD_MISMATCH -0x1380 /**< Given private key password does not allow for correct decryption. */ |
| 45 | #define POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE -0x1400 /**< Unavailable feature, e.g. hashing/encryption combination. */ |
Paul Bakker | 00b2860 | 2013-06-24 13:02:41 +0200 | [diff] [blame] | 46 | #define POLARSSL_ERR_PEM_BAD_INPUT_DATA -0x1480 /**< Bad input parameters to function. */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 47 | /* \} name */ |
| 48 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 49 | #ifdef __cplusplus |
| 50 | extern "C" { |
| 51 | #endif |
| 52 | |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame^] | 53 | #if defined(POLARSSL_PEM_PARSE_C) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 54 | /** |
| 55 | * \brief PEM context structure |
| 56 | */ |
| 57 | typedef struct |
| 58 | { |
| 59 | unsigned char *buf; /*!< buffer for decoded data */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 60 | size_t buflen; /*!< length of the buffer */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 61 | unsigned char *info; /*!< buffer for extra header information */ |
| 62 | } |
| 63 | pem_context; |
| 64 | |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 65 | /** |
| 66 | * \brief PEM context setup |
| 67 | * |
| 68 | * \param ctx context to be initialized |
| 69 | */ |
| 70 | void pem_init( pem_context *ctx ); |
| 71 | |
| 72 | /** |
| 73 | * \brief Read a buffer for PEM information and store the resulting |
| 74 | * data into the specified context buffers. |
| 75 | * |
| 76 | * \param ctx context to use |
| 77 | * \param header header string to seek and expect |
| 78 | * \param footer footer string to seek and expect |
| 79 | * \param data source data to look in |
| 80 | * \param pwd password for decryption (can be NULL) |
| 81 | * \param pwdlen length of password |
Paul Bakker | 00b2860 | 2013-06-24 13:02:41 +0200 | [diff] [blame] | 82 | * \param use_len destination for total length used (set after header is |
| 83 | * correctly read, so unless you get |
| 84 | * POLARSSL_ERR_PEM_BAD_INPUT_DATA or |
| 85 | * POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT, use_len is |
| 86 | * the length to skip) |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 87 | * |
Manuel Pégourié-Gonnard | 7d4e5b7 | 2013-07-09 16:35:23 +0200 | [diff] [blame] | 88 | * \note Attempts to check password correctness by verifying if |
| 89 | * the decrypted text starts with an ASN.1 sequence of |
| 90 | * appropriate length |
Manuel Pégourié-Gonnard | f8648d5 | 2013-07-03 21:01:35 +0200 | [diff] [blame] | 91 | * |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 92 | * \return 0 on success, or a specific PEM error code |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 93 | */ |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 94 | int pem_read_buffer( pem_context *ctx, const char *header, const char *footer, |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 95 | const unsigned char *data, |
| 96 | const unsigned char *pwd, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 97 | size_t pwdlen, size_t *use_len ); |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 98 | |
| 99 | /** |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame^] | 100 | * \brief PEM context memory freeing |
| 101 | * |
| 102 | * \param ctx context to be freed |
| 103 | */ |
| 104 | void pem_free( pem_context *ctx ); |
| 105 | #endif /* POLARSSL_PEM_PARSE_C */ |
| 106 | |
| 107 | #if defined(POLARSSL_PEM_WRITE_C) |
| 108 | /** |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 109 | * \brief Write a buffer of PEM information from a DER encoded |
| 110 | * buffer. |
| 111 | * |
| 112 | * \param header header string to write |
| 113 | * \param footer footer string to write |
| 114 | * \param der_data DER data to write |
| 115 | * \param der_len length of the DER data |
| 116 | * \param buf buffer to write to |
| 117 | * \param buf_len length of output buffer |
| 118 | * \param olen total length written / required (if buf_len is not enough) |
| 119 | * |
| 120 | * \return 0 on success, or a specific PEM or BASE64 error code. On |
| 121 | * POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL olen is the required |
| 122 | * size. |
| 123 | */ |
| 124 | int pem_write_buffer( const char *header, const char *footer, |
| 125 | const unsigned char *der_data, size_t der_len, |
| 126 | unsigned char *buf, size_t buf_len, size_t *olen ); |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame^] | 127 | #endif /* POLARSSL_PEM_WRITE_C */ |
Paul Bakker | 96743fc | 2011-02-12 14:30:57 +0000 | [diff] [blame] | 128 | |
| 129 | #ifdef __cplusplus |
| 130 | } |
| 131 | #endif |
| 132 | |
| 133 | #endif /* pem.h */ |