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