mbed TLS Build
include/mbedtls/pem.h@0:cdf462088d13, 2017-01-05 (annotated)
- Committer:
- markrad
- Date:
- Thu Jan 05 00:18:44 2017 +0000
- Revision:
- 0:cdf462088d13
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | /** |
markrad | 0:cdf462088d13 | 2 | * \file pem.h |
markrad | 0:cdf462088d13 | 3 | * |
markrad | 0:cdf462088d13 | 4 | * \brief Privacy Enhanced Mail (PEM) decoding |
markrad | 0:cdf462088d13 | 5 | * |
markrad | 0:cdf462088d13 | 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
markrad | 0:cdf462088d13 | 7 | * SPDX-License-Identifier: Apache-2.0 |
markrad | 0:cdf462088d13 | 8 | * |
markrad | 0:cdf462088d13 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
markrad | 0:cdf462088d13 | 10 | * not use this file except in compliance with the License. |
markrad | 0:cdf462088d13 | 11 | * You may obtain a copy of the License at |
markrad | 0:cdf462088d13 | 12 | * |
markrad | 0:cdf462088d13 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
markrad | 0:cdf462088d13 | 14 | * |
markrad | 0:cdf462088d13 | 15 | * Unless required by applicable law or agreed to in writing, software |
markrad | 0:cdf462088d13 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
markrad | 0:cdf462088d13 | 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
markrad | 0:cdf462088d13 | 18 | * See the License for the specific language governing permissions and |
markrad | 0:cdf462088d13 | 19 | * limitations under the License. |
markrad | 0:cdf462088d13 | 20 | * |
markrad | 0:cdf462088d13 | 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
markrad | 0:cdf462088d13 | 22 | */ |
markrad | 0:cdf462088d13 | 23 | #ifndef MBEDTLS_PEM_H |
markrad | 0:cdf462088d13 | 24 | #define MBEDTLS_PEM_H |
markrad | 0:cdf462088d13 | 25 | |
markrad | 0:cdf462088d13 | 26 | #include <stddef.h> |
markrad | 0:cdf462088d13 | 27 | |
markrad | 0:cdf462088d13 | 28 | /** |
markrad | 0:cdf462088d13 | 29 | * \name PEM Error codes |
markrad | 0:cdf462088d13 | 30 | * These error codes are returned in case of errors reading the |
markrad | 0:cdf462088d13 | 31 | * PEM data. |
markrad | 0:cdf462088d13 | 32 | * \{ |
markrad | 0:cdf462088d13 | 33 | */ |
markrad | 0:cdf462088d13 | 34 | #define MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT -0x1080 /**< No PEM header or footer found. */ |
markrad | 0:cdf462088d13 | 35 | #define MBEDTLS_ERR_PEM_INVALID_DATA -0x1100 /**< PEM string is not as expected. */ |
markrad | 0:cdf462088d13 | 36 | #define MBEDTLS_ERR_PEM_ALLOC_FAILED -0x1180 /**< Failed to allocate memory. */ |
markrad | 0:cdf462088d13 | 37 | #define MBEDTLS_ERR_PEM_INVALID_ENC_IV -0x1200 /**< RSA IV is not in hex-format. */ |
markrad | 0:cdf462088d13 | 38 | #define MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG -0x1280 /**< Unsupported key encryption algorithm. */ |
markrad | 0:cdf462088d13 | 39 | #define MBEDTLS_ERR_PEM_PASSWORD_REQUIRED -0x1300 /**< Private key password can't be empty. */ |
markrad | 0:cdf462088d13 | 40 | #define MBEDTLS_ERR_PEM_PASSWORD_MISMATCH -0x1380 /**< Given private key password does not allow for correct decryption. */ |
markrad | 0:cdf462088d13 | 41 | #define MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE -0x1400 /**< Unavailable feature, e.g. hashing/encryption combination. */ |
markrad | 0:cdf462088d13 | 42 | #define MBEDTLS_ERR_PEM_BAD_INPUT_DATA -0x1480 /**< Bad input parameters to function. */ |
markrad | 0:cdf462088d13 | 43 | /* \} name */ |
markrad | 0:cdf462088d13 | 44 | |
markrad | 0:cdf462088d13 | 45 | #ifdef __cplusplus |
markrad | 0:cdf462088d13 | 46 | extern "C" { |
markrad | 0:cdf462088d13 | 47 | #endif |
markrad | 0:cdf462088d13 | 48 | |
markrad | 0:cdf462088d13 | 49 | #if defined(MBEDTLS_PEM_PARSE_C) |
markrad | 0:cdf462088d13 | 50 | /** |
markrad | 0:cdf462088d13 | 51 | * \brief PEM context structure |
markrad | 0:cdf462088d13 | 52 | */ |
markrad | 0:cdf462088d13 | 53 | typedef struct |
markrad | 0:cdf462088d13 | 54 | { |
markrad | 0:cdf462088d13 | 55 | unsigned char *buf; /*!< buffer for decoded data */ |
markrad | 0:cdf462088d13 | 56 | size_t buflen; /*!< length of the buffer */ |
markrad | 0:cdf462088d13 | 57 | unsigned char *info; /*!< buffer for extra header information */ |
markrad | 0:cdf462088d13 | 58 | } |
markrad | 0:cdf462088d13 | 59 | mbedtls_pem_context; |
markrad | 0:cdf462088d13 | 60 | |
markrad | 0:cdf462088d13 | 61 | /** |
markrad | 0:cdf462088d13 | 62 | * \brief PEM context setup |
markrad | 0:cdf462088d13 | 63 | * |
markrad | 0:cdf462088d13 | 64 | * \param ctx context to be initialized |
markrad | 0:cdf462088d13 | 65 | */ |
markrad | 0:cdf462088d13 | 66 | void mbedtls_pem_init( mbedtls_pem_context *ctx ); |
markrad | 0:cdf462088d13 | 67 | |
markrad | 0:cdf462088d13 | 68 | /** |
markrad | 0:cdf462088d13 | 69 | * \brief Read a buffer for PEM information and store the resulting |
markrad | 0:cdf462088d13 | 70 | * data into the specified context buffers. |
markrad | 0:cdf462088d13 | 71 | * |
markrad | 0:cdf462088d13 | 72 | * \param ctx context to use |
markrad | 0:cdf462088d13 | 73 | * \param header header string to seek and expect |
markrad | 0:cdf462088d13 | 74 | * \param footer footer string to seek and expect |
markrad | 0:cdf462088d13 | 75 | * \param data source data to look in (must be nul-terminated) |
markrad | 0:cdf462088d13 | 76 | * \param pwd password for decryption (can be NULL) |
markrad | 0:cdf462088d13 | 77 | * \param pwdlen length of password |
markrad | 0:cdf462088d13 | 78 | * \param use_len destination for total length used (set after header is |
markrad | 0:cdf462088d13 | 79 | * correctly read, so unless you get |
markrad | 0:cdf462088d13 | 80 | * MBEDTLS_ERR_PEM_BAD_INPUT_DATA or |
markrad | 0:cdf462088d13 | 81 | * MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT, use_len is |
markrad | 0:cdf462088d13 | 82 | * the length to skip) |
markrad | 0:cdf462088d13 | 83 | * |
markrad | 0:cdf462088d13 | 84 | * \note Attempts to check password correctness by verifying if |
markrad | 0:cdf462088d13 | 85 | * the decrypted text starts with an ASN.1 sequence of |
markrad | 0:cdf462088d13 | 86 | * appropriate length |
markrad | 0:cdf462088d13 | 87 | * |
markrad | 0:cdf462088d13 | 88 | * \return 0 on success, or a specific PEM error code |
markrad | 0:cdf462088d13 | 89 | */ |
markrad | 0:cdf462088d13 | 90 | int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer, |
markrad | 0:cdf462088d13 | 91 | const unsigned char *data, |
markrad | 0:cdf462088d13 | 92 | const unsigned char *pwd, |
markrad | 0:cdf462088d13 | 93 | size_t pwdlen, size_t *use_len ); |
markrad | 0:cdf462088d13 | 94 | |
markrad | 0:cdf462088d13 | 95 | /** |
markrad | 0:cdf462088d13 | 96 | * \brief PEM context memory freeing |
markrad | 0:cdf462088d13 | 97 | * |
markrad | 0:cdf462088d13 | 98 | * \param ctx context to be freed |
markrad | 0:cdf462088d13 | 99 | */ |
markrad | 0:cdf462088d13 | 100 | void mbedtls_pem_free( mbedtls_pem_context *ctx ); |
markrad | 0:cdf462088d13 | 101 | #endif /* MBEDTLS_PEM_PARSE_C */ |
markrad | 0:cdf462088d13 | 102 | |
markrad | 0:cdf462088d13 | 103 | #if defined(MBEDTLS_PEM_WRITE_C) |
markrad | 0:cdf462088d13 | 104 | /** |
markrad | 0:cdf462088d13 | 105 | * \brief Write a buffer of PEM information from a DER encoded |
markrad | 0:cdf462088d13 | 106 | * buffer. |
markrad | 0:cdf462088d13 | 107 | * |
markrad | 0:cdf462088d13 | 108 | * \param header header string to write |
markrad | 0:cdf462088d13 | 109 | * \param footer footer string to write |
markrad | 0:cdf462088d13 | 110 | * \param der_data DER data to write |
markrad | 0:cdf462088d13 | 111 | * \param der_len length of the DER data |
markrad | 0:cdf462088d13 | 112 | * \param buf buffer to write to |
markrad | 0:cdf462088d13 | 113 | * \param buf_len length of output buffer |
markrad | 0:cdf462088d13 | 114 | * \param olen total length written / required (if buf_len is not enough) |
markrad | 0:cdf462088d13 | 115 | * |
markrad | 0:cdf462088d13 | 116 | * \return 0 on success, or a specific PEM or BASE64 error code. On |
markrad | 0:cdf462088d13 | 117 | * MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL olen is the required |
markrad | 0:cdf462088d13 | 118 | * size. |
markrad | 0:cdf462088d13 | 119 | */ |
markrad | 0:cdf462088d13 | 120 | int mbedtls_pem_write_buffer( const char *header, const char *footer, |
markrad | 0:cdf462088d13 | 121 | const unsigned char *der_data, size_t der_len, |
markrad | 0:cdf462088d13 | 122 | unsigned char *buf, size_t buf_len, size_t *olen ); |
markrad | 0:cdf462088d13 | 123 | #endif /* MBEDTLS_PEM_WRITE_C */ |
markrad | 0:cdf462088d13 | 124 | |
markrad | 0:cdf462088d13 | 125 | #ifdef __cplusplus |
markrad | 0:cdf462088d13 | 126 | } |
markrad | 0:cdf462088d13 | 127 | #endif |
markrad | 0:cdf462088d13 | 128 | |
markrad | 0:cdf462088d13 | 129 | #endif /* pem.h */ |