Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.  * \file pem.h
  3.  *
  4.  * \brief Privacy Enhanced Mail (PEM) decoding
  5.  */
  6. /*
  7.  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8.  *  SPDX-License-Identifier: GPL-2.0
  9.  *
  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.  *  This file is part of mbed TLS (https://tls.mbed.org)
  25.  */
  26. #ifndef MBEDTLS_PEM_H
  27. #define MBEDTLS_PEM_H
  28.  
  29. #if !defined(MBEDTLS_CONFIG_FILE)
  30. #include "config.h"
  31. #else
  32. #include MBEDTLS_CONFIG_FILE
  33. #endif
  34.  
  35. #include <stddef.h>
  36.  
  37. /**
  38.  * \name PEM Error codes
  39.  * These error codes are returned in case of errors reading the
  40.  * PEM data.
  41.  * \{
  42.  */
  43. #define MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT          -0x1080  /**< No PEM header or footer found. */
  44. #define MBEDTLS_ERR_PEM_INVALID_DATA                      -0x1100  /**< PEM string is not as expected. */
  45. #define MBEDTLS_ERR_PEM_ALLOC_FAILED                      -0x1180  /**< Failed to allocate memory. */
  46. #define MBEDTLS_ERR_PEM_INVALID_ENC_IV                    -0x1200  /**< RSA IV is not in hex-format. */
  47. #define MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG                   -0x1280  /**< Unsupported key encryption algorithm. */
  48. #define MBEDTLS_ERR_PEM_PASSWORD_REQUIRED                 -0x1300  /**< Private key password can't be empty. */
  49. #define MBEDTLS_ERR_PEM_PASSWORD_MISMATCH                 -0x1380  /**< Given private key password does not allow for correct decryption. */
  50. #define MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE               -0x1400  /**< Unavailable feature, e.g. hashing/encryption combination. */
  51. #define MBEDTLS_ERR_PEM_BAD_INPUT_DATA                    -0x1480  /**< Bad input parameters to function. */
  52. /* \} name */
  53.  
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57.  
  58. #if defined(MBEDTLS_PEM_PARSE_C)
  59. /**
  60.  * \brief       PEM context structure
  61.  */
  62. typedef struct mbedtls_pem_context
  63. {
  64.     unsigned char *buf;     /*!< buffer for decoded data             */
  65.     size_t buflen;          /*!< length of the buffer                */
  66.     unsigned char *info;    /*!< buffer for extra header information */
  67. }
  68. mbedtls_pem_context;
  69.  
  70. /**
  71.  * \brief       PEM context setup
  72.  *
  73.  * \param ctx   context to be initialized
  74.  */
  75. void mbedtls_pem_init( mbedtls_pem_context *ctx );
  76.  
  77. /**
  78.  * \brief       Read a buffer for PEM information and store the resulting
  79.  *              data into the specified context buffers.
  80.  *
  81.  * \param ctx       context to use
  82.  * \param header    header string to seek and expect
  83.  * \param footer    footer string to seek and expect
  84.  * \param data      source data to look in (must be nul-terminated)
  85.  * \param pwd       password for decryption (can be NULL)
  86.  * \param pwdlen    length of password
  87.  * \param use_len   destination for total length used (set after header is
  88.  *                  correctly read, so unless you get
  89.  *                  MBEDTLS_ERR_PEM_BAD_INPUT_DATA or
  90.  *                  MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT, use_len is
  91.  *                  the length to skip)
  92.  *
  93.  * \note            Attempts to check password correctness by verifying if
  94.  *                  the decrypted text starts with an ASN.1 sequence of
  95.  *                  appropriate length
  96.  *
  97.  * \return          0 on success, or a specific PEM error code
  98.  */
  99. int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
  100.                      const unsigned char *data,
  101.                      const unsigned char *pwd,
  102.                      size_t pwdlen, size_t *use_len );
  103.  
  104. /**
  105.  * \brief       PEM context memory freeing
  106.  *
  107.  * \param ctx   context to be freed
  108.  */
  109. void mbedtls_pem_free( mbedtls_pem_context *ctx );
  110. #endif /* MBEDTLS_PEM_PARSE_C */
  111.  
  112. #if defined(MBEDTLS_PEM_WRITE_C)
  113. /**
  114.  * \brief           Write a buffer of PEM information from a DER encoded
  115.  *                  buffer.
  116.  *
  117.  * \param header    header string to write
  118.  * \param footer    footer string to write
  119.  * \param der_data  DER data to write
  120.  * \param der_len   length of the DER data
  121.  * \param buf       buffer to write to
  122.  * \param buf_len   length of output buffer
  123.  * \param olen      total length written / required (if buf_len is not enough)
  124.  *
  125.  * \return          0 on success, or a specific PEM or BASE64 error code. On
  126.  *                  MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL olen is the required
  127.  *                  size.
  128.  */
  129. int mbedtls_pem_write_buffer( const char *header, const char *footer,
  130.                       const unsigned char *der_data, size_t der_len,
  131.                       unsigned char *buf, size_t buf_len, size_t *olen );
  132. #endif /* MBEDTLS_PEM_WRITE_C */
  133.  
  134. #ifdef __cplusplus
  135. }
  136. #endif
  137.  
  138. #endif /* pem.h */
  139.