Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pem.h Source File

pem.h

Go to the documentation of this file.
00001 /**
00002  * \file pem.h
00003  *
00004  * \brief Privacy Enhanced Mail (PEM) decoding
00005  */
00006 /*
00007  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00008  *  SPDX-License-Identifier: Apache-2.0
00009  *
00010  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00011  *  not use this file except in compliance with the License.
00012  *  You may obtain a copy of the License at
00013  *
00014  *  http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  *  Unless required by applicable law or agreed to in writing, software
00017  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00018  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  *  See the License for the specific language governing permissions and
00020  *  limitations under the License.
00021  *
00022  *  This file is part of mbed TLS (https://tls.mbed.org)
00023  */
00024 #ifndef MBEDTLS_PEM_H
00025 #define MBEDTLS_PEM_H
00026 
00027 #if !defined(MBEDTLS_CONFIG_FILE)
00028 #include "mbedtls/config.h"
00029 #else
00030 #include MBEDTLS_CONFIG_FILE
00031 #endif
00032 
00033 #include <stddef.h>
00034 
00035 /**
00036  * \name PEM Error codes
00037  * These error codes are returned in case of errors reading the
00038  * PEM data.
00039  * \{
00040  */
00041 #define MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT          -0x1080  /**< No PEM header or footer found. */
00042 #define MBEDTLS_ERR_PEM_INVALID_DATA                      -0x1100  /**< PEM string is not as expected. */
00043 #define MBEDTLS_ERR_PEM_ALLOC_FAILED                      -0x1180  /**< Failed to allocate memory. */
00044 #define MBEDTLS_ERR_PEM_INVALID_ENC_IV                    -0x1200  /**< RSA IV is not in hex-format. */
00045 #define MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG                   -0x1280  /**< Unsupported key encryption algorithm. */
00046 #define MBEDTLS_ERR_PEM_PASSWORD_REQUIRED                 -0x1300  /**< Private key password can't be empty. */
00047 #define MBEDTLS_ERR_PEM_PASSWORD_MISMATCH                 -0x1380  /**< Given private key password does not allow for correct decryption. */
00048 #define MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE               -0x1400  /**< Unavailable feature, e.g. hashing/encryption combination. */
00049 #define MBEDTLS_ERR_PEM_BAD_INPUT_DATA                    -0x1480  /**< Bad input parameters to function. */
00050 /* \} name */
00051 
00052 #ifdef __cplusplus
00053 extern "C" {
00054 #endif
00055 
00056 #if defined(MBEDTLS_PEM_PARSE_C)
00057 /**
00058  * \brief       PEM context structure
00059  */
00060 typedef struct mbedtls_pem_context
00061 {
00062     unsigned char *buf ;     /*!< buffer for decoded data             */
00063     size_t buflen ;          /*!< length of the buffer                */
00064     unsigned char *info ;    /*!< buffer for extra header information */
00065 }
00066 mbedtls_pem_context;
00067 
00068 /**
00069  * \brief       PEM context setup
00070  *
00071  * \param ctx   context to be initialized
00072  */
00073 void mbedtls_pem_init( mbedtls_pem_context *ctx );
00074 
00075 /**
00076  * \brief       Read a buffer for PEM information and store the resulting
00077  *              data into the specified context buffers.
00078  *
00079  * \param ctx       context to use
00080  * \param header    header string to seek and expect
00081  * \param footer    footer string to seek and expect
00082  * \param data      source data to look in (must be nul-terminated)
00083  * \param pwd       password for decryption (can be NULL)
00084  * \param pwdlen    length of password
00085  * \param use_len   destination for total length used (set after header is
00086  *                  correctly read, so unless you get
00087  *                  MBEDTLS_ERR_PEM_BAD_INPUT_DATA or
00088  *                  MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT, use_len is
00089  *                  the length to skip)
00090  *
00091  * \note            Attempts to check password correctness by verifying if
00092  *                  the decrypted text starts with an ASN.1 sequence of
00093  *                  appropriate length
00094  *
00095  * \return          0 on success, or a specific PEM error code
00096  */
00097 int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
00098                      const unsigned char *data,
00099                      const unsigned char *pwd,
00100                      size_t pwdlen, size_t *use_len );
00101 
00102 /**
00103  * \brief       PEM context memory freeing
00104  *
00105  * \param ctx   context to be freed
00106  */
00107 void mbedtls_pem_free( mbedtls_pem_context *ctx );
00108 #endif /* MBEDTLS_PEM_PARSE_C */
00109 
00110 #if defined(MBEDTLS_PEM_WRITE_C)
00111 /**
00112  * \brief           Write a buffer of PEM information from a DER encoded
00113  *                  buffer.
00114  *
00115  * \param header    The header string to write.
00116  * \param footer    The footer string to write.
00117  * \param der_data  The DER data to encode.
00118  * \param der_len   The length of the DER data \p der_data in Bytes.
00119  * \param buf       The buffer to write to.
00120  * \param buf_len   The length of the output buffer \p buf in Bytes.
00121  * \param olen      The address at which to store the total length written
00122  *                  or required (if \p buf_len is not enough).
00123  *
00124  * \note            You may pass \c NULL for \p buf and \c 0 for \p buf_len
00125  *                  to request the length of the resulting PEM buffer in
00126  *                  `*olen`.
00127  *
00128  * \note            This function may be called with overlapping \p der_data
00129  *                  and \p buf buffers.
00130  *
00131  * \return          \c 0 on success.
00132  * \return          #MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL if \p buf isn't large
00133  *                  enough to hold the PEM buffer. In  this case, `*olen` holds
00134  *                  the required minimum size of \p buf.
00135  * \return          Another PEM or BASE64 error code on other kinds of failure.
00136  */
00137 int mbedtls_pem_write_buffer( const char *header, const char *footer,
00138                       const unsigned char *der_data, size_t der_len,
00139                       unsigned char *buf, size_t buf_len, size_t *olen );
00140 #endif /* MBEDTLS_PEM_WRITE_C */
00141 
00142 #ifdef __cplusplus
00143 }
00144 #endif
00145 
00146 #endif /* pem.h */