Subversion Repositories Kolibri OS

Rev

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

  1. /**
  2.  * \file x509_csr.h
  3.  *
  4.  * \brief X.509 certificate signing request parsing and writing
  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_X509_CSR_H
  27. #define MBEDTLS_X509_CSR_H
  28.  
  29. #if !defined(MBEDTLS_CONFIG_FILE)
  30. #include "config.h"
  31. #else
  32. #include MBEDTLS_CONFIG_FILE
  33. #endif
  34.  
  35. #include "x509.h"
  36.  
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40.  
  41. /**
  42.  * \addtogroup x509_module
  43.  * \{ */
  44.  
  45. /**
  46.  * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
  47.  * \{
  48.  */
  49.  
  50. /**
  51.  * Certificate Signing Request (CSR) structure.
  52.  */
  53. typedef struct mbedtls_x509_csr
  54. {
  55.     mbedtls_x509_buf raw;           /**< The raw CSR data (DER). */
  56.     mbedtls_x509_buf cri;           /**< The raw CertificateRequestInfo body (DER). */
  57.  
  58.     int version;            /**< CSR version (1=v1). */
  59.  
  60.     mbedtls_x509_buf  subject_raw;  /**< The raw subject data (DER). */
  61.     mbedtls_x509_name subject;      /**< The parsed subject data (named information object). */
  62.  
  63.     mbedtls_pk_context pk;          /**< Container for the public key context. */
  64.  
  65.     mbedtls_x509_buf sig_oid;
  66.     mbedtls_x509_buf sig;
  67.     mbedtls_md_type_t sig_md;       /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
  68.     mbedtls_pk_type_t sig_pk;       /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
  69.     void *sig_opts;         /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
  70. }
  71. mbedtls_x509_csr;
  72.  
  73. /**
  74.  * Container for writing a CSR
  75.  */
  76. typedef struct mbedtls_x509write_csr
  77. {
  78.     mbedtls_pk_context *key;
  79.     mbedtls_asn1_named_data *subject;
  80.     mbedtls_md_type_t md_alg;
  81.     mbedtls_asn1_named_data *extensions;
  82. }
  83. mbedtls_x509write_csr;
  84.  
  85. #if defined(MBEDTLS_X509_CSR_PARSE_C)
  86. /**
  87.  * \brief          Load a Certificate Signing Request (CSR) in DER format
  88.  *
  89.  * \note           CSR attributes (if any) are currently silently ignored.
  90.  *
  91.  * \param csr      CSR context to fill
  92.  * \param buf      buffer holding the CRL data
  93.  * \param buflen   size of the buffer
  94.  *
  95.  * \return         0 if successful, or a specific X509 error code
  96.  */
  97. int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
  98.                         const unsigned char *buf, size_t buflen );
  99.  
  100. /**
  101.  * \brief          Load a Certificate Signing Request (CSR), DER or PEM format
  102.  *
  103.  * \note           See notes for \c mbedtls_x509_csr_parse_der()
  104.  *
  105.  * \param csr      CSR context to fill
  106.  * \param buf      buffer holding the CRL data
  107.  * \param buflen   size of the buffer
  108.  *                 (including the terminating null byte for PEM data)
  109.  *
  110.  * \return         0 if successful, or a specific X509 or PEM error code
  111.  */
  112. int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen );
  113.  
  114. #if defined(MBEDTLS_FS_IO)
  115. /**
  116.  * \brief          Load a Certificate Signing Request (CSR)
  117.  *
  118.  * \note           See notes for \c mbedtls_x509_csr_parse()
  119.  *
  120.  * \param csr      CSR context to fill
  121.  * \param path     filename to read the CSR from
  122.  *
  123.  * \return         0 if successful, or a specific X509 or PEM error code
  124.  */
  125. int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path );
  126. #endif /* MBEDTLS_FS_IO */
  127.  
  128. /**
  129.  * \brief          Returns an informational string about the
  130.  *                 CSR.
  131.  *
  132.  * \param buf      Buffer to write to
  133.  * \param size     Maximum size of buffer
  134.  * \param prefix   A line prefix
  135.  * \param csr      The X509 CSR to represent
  136.  *
  137.  * \return         The length of the string written (not including the
  138.  *                 terminated nul byte), or a negative error code.
  139.  */
  140. int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
  141.                    const mbedtls_x509_csr *csr );
  142.  
  143. /**
  144.  * \brief          Initialize a CSR
  145.  *
  146.  * \param csr      CSR to initialize
  147.  */
  148. void mbedtls_x509_csr_init( mbedtls_x509_csr *csr );
  149.  
  150. /**
  151.  * \brief          Unallocate all CSR data
  152.  *
  153.  * \param csr      CSR to free
  154.  */
  155. void mbedtls_x509_csr_free( mbedtls_x509_csr *csr );
  156. #endif /* MBEDTLS_X509_CSR_PARSE_C */
  157.  
  158. /* \} name */
  159. /* \} addtogroup x509_module */
  160.  
  161. #if defined(MBEDTLS_X509_CSR_WRITE_C)
  162. /**
  163.  * \brief           Initialize a CSR context
  164.  *
  165.  * \param ctx       CSR context to initialize
  166.  */
  167. void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx );
  168.  
  169. /**
  170.  * \brief           Set the subject name for a CSR
  171.  *                  Subject names should contain a comma-separated list
  172.  *                  of OID types and values:
  173.  *                  e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
  174.  *
  175.  * \param ctx           CSR context to use
  176.  * \param subject_name  subject name to set
  177.  *
  178.  * \return          0 if subject name was parsed successfully, or
  179.  *                  a specific error code
  180.  */
  181. int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
  182.                                     const char *subject_name );
  183.  
  184. /**
  185.  * \brief           Set the key for a CSR (public key will be included,
  186.  *                  private key used to sign the CSR when writing it)
  187.  *
  188.  * \param ctx       CSR context to use
  189.  * \param key       Asymetric key to include
  190.  */
  191. void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key );
  192.  
  193. /**
  194.  * \brief           Set the MD algorithm to use for the signature
  195.  *                  (e.g. MBEDTLS_MD_SHA1)
  196.  *
  197.  * \param ctx       CSR context to use
  198.  * \param md_alg    MD algorithm to use
  199.  */
  200. void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg );
  201.  
  202. /**
  203.  * \brief           Set the Key Usage Extension flags
  204.  *                  (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
  205.  *
  206.  * \param ctx       CSR context to use
  207.  * \param key_usage key usage flags to set
  208.  *
  209.  * \return          0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  210.  *
  211.  * \note            The <code>decipherOnly</code> flag from the Key Usage
  212.  *                  extension is represented by bit 8 (i.e.
  213.  *                  <code>0x8000</code>), which cannot typically be represented
  214.  *                  in an unsigned char. Therefore, the flag
  215.  *                  <code>decipherOnly</code> (i.e.
  216.  *                  #MBEDTLS_X509_KU_DECIPHER_ONLY) cannot be set using this
  217.  *                  function.
  218.  */
  219. int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage );
  220.  
  221. /**
  222.  * \brief           Set the Netscape Cert Type flags
  223.  *                  (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
  224.  *
  225.  * \param ctx           CSR context to use
  226.  * \param ns_cert_type  Netscape Cert Type flags to set
  227.  *
  228.  * \return          0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  229.  */
  230. int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
  231.                                     unsigned char ns_cert_type );
  232.  
  233. /**
  234.  * \brief           Generic function to add to or replace an extension in the
  235.  *                  CSR
  236.  *
  237.  * \param ctx       CSR context to use
  238.  * \param oid       OID of the extension
  239.  * \param oid_len   length of the OID
  240.  * \param val       value of the extension OCTET STRING
  241.  * \param val_len   length of the value data
  242.  *
  243.  * \return          0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  244.  */
  245. int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
  246.                                  const char *oid, size_t oid_len,
  247.                                  const unsigned char *val, size_t val_len );
  248.  
  249. /**
  250.  * \brief           Free the contents of a CSR context
  251.  *
  252.  * \param ctx       CSR context to free
  253.  */
  254. void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx );
  255.  
  256. /**
  257.  * \brief           Write a CSR (Certificate Signing Request) to a
  258.  *                  DER structure
  259.  *                  Note: data is written at the end of the buffer! Use the
  260.  *                        return value to determine where you should start
  261.  *                        using the buffer
  262.  *
  263.  * \param ctx       CSR to write away
  264.  * \param buf       buffer to write to
  265.  * \param size      size of the buffer
  266.  * \param f_rng     RNG function (for signature, see note)
  267.  * \param p_rng     RNG parameter
  268.  *
  269.  * \return          length of data written if successful, or a specific
  270.  *                  error code
  271.  *
  272.  * \note            f_rng may be NULL if RSA is used for signature and the
  273.  *                  signature is made offline (otherwise f_rng is desirable
  274.  *                  for countermeasures against timing attacks).
  275.  *                  ECDSA signatures always require a non-NULL f_rng.
  276.  */
  277. int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  278.                        int (*f_rng)(void *, unsigned char *, size_t),
  279.                        void *p_rng );
  280.  
  281. #if defined(MBEDTLS_PEM_WRITE_C)
  282. /**
  283.  * \brief           Write a CSR (Certificate Signing Request) to a
  284.  *                  PEM string
  285.  *
  286.  * \param ctx       CSR to write away
  287.  * \param buf       buffer to write to
  288.  * \param size      size of the buffer
  289.  * \param f_rng     RNG function (for signature, see note)
  290.  * \param p_rng     RNG parameter
  291.  *
  292.  * \return          0 if successful, or a specific error code
  293.  *
  294.  * \note            f_rng may be NULL if RSA is used for signature and the
  295.  *                  signature is made offline (otherwise f_rng is desirable
  296.  *                  for countermeasures against timing attacks).
  297.  *                  ECDSA signatures always require a non-NULL f_rng.
  298.  */
  299. int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  300.                        int (*f_rng)(void *, unsigned char *, size_t),
  301.                        void *p_rng );
  302. #endif /* MBEDTLS_PEM_WRITE_C */
  303. #endif /* MBEDTLS_X509_CSR_WRITE_C */
  304.  
  305. #ifdef __cplusplus
  306. }
  307. #endif
  308.  
  309. #endif /* mbedtls_x509_csr.h */
  310.