Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

Committer:
HannesTschofenig
Date:
Thu Sep 27 06:34:22 2018 +0000
Revision:
0:796d0f61a05b
Example AES-GCM test program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HannesTschofenig 0:796d0f61a05b 1 /**
HannesTschofenig 0:796d0f61a05b 2 * \file x509_csr.h
HannesTschofenig 0:796d0f61a05b 3 *
HannesTschofenig 0:796d0f61a05b 4 * \brief X.509 certificate signing request parsing and writing
HannesTschofenig 0:796d0f61a05b 5 *
HannesTschofenig 0:796d0f61a05b 6 * Copyright (C) 2006-2014, Brainspark B.V.
HannesTschofenig 0:796d0f61a05b 7 *
HannesTschofenig 0:796d0f61a05b 8 * This file is part of PolarSSL (http://www.polarssl.org)
HannesTschofenig 0:796d0f61a05b 9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
HannesTschofenig 0:796d0f61a05b 10 *
HannesTschofenig 0:796d0f61a05b 11 * All rights reserved.
HannesTschofenig 0:796d0f61a05b 12 *
HannesTschofenig 0:796d0f61a05b 13 * This program is free software; you can redistribute it and/or modify
HannesTschofenig 0:796d0f61a05b 14 * it under the terms of the GNU General Public License as published by
HannesTschofenig 0:796d0f61a05b 15 * the Free Software Foundation; either version 2 of the License, or
HannesTschofenig 0:796d0f61a05b 16 * (at your option) any later version.
HannesTschofenig 0:796d0f61a05b 17 *
HannesTschofenig 0:796d0f61a05b 18 * This program is distributed in the hope that it will be useful,
HannesTschofenig 0:796d0f61a05b 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
HannesTschofenig 0:796d0f61a05b 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
HannesTschofenig 0:796d0f61a05b 21 * GNU General Public License for more details.
HannesTschofenig 0:796d0f61a05b 22 *
HannesTschofenig 0:796d0f61a05b 23 * You should have received a copy of the GNU General Public License along
HannesTschofenig 0:796d0f61a05b 24 * with this program; if not, write to the Free Software Foundation, Inc.,
HannesTschofenig 0:796d0f61a05b 25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
HannesTschofenig 0:796d0f61a05b 26 */
HannesTschofenig 0:796d0f61a05b 27 #ifndef POLARSSL_X509_CSR_H
HannesTschofenig 0:796d0f61a05b 28 #define POLARSSL_X509_CSR_H
HannesTschofenig 0:796d0f61a05b 29
HannesTschofenig 0:796d0f61a05b 30 #if !defined(POLARSSL_CONFIG_FILE)
HannesTschofenig 0:796d0f61a05b 31 #include "config.h"
HannesTschofenig 0:796d0f61a05b 32 #else
HannesTschofenig 0:796d0f61a05b 33 #include POLARSSL_CONFIG_FILE
HannesTschofenig 0:796d0f61a05b 34 #endif
HannesTschofenig 0:796d0f61a05b 35
HannesTschofenig 0:796d0f61a05b 36 #include "x509.h"
HannesTschofenig 0:796d0f61a05b 37
HannesTschofenig 0:796d0f61a05b 38 #ifdef __cplusplus
HannesTschofenig 0:796d0f61a05b 39 extern "C" {
HannesTschofenig 0:796d0f61a05b 40 #endif
HannesTschofenig 0:796d0f61a05b 41
HannesTschofenig 0:796d0f61a05b 42 /**
HannesTschofenig 0:796d0f61a05b 43 * \addtogroup x509_module
HannesTschofenig 0:796d0f61a05b 44 * \{ */
HannesTschofenig 0:796d0f61a05b 45
HannesTschofenig 0:796d0f61a05b 46 /**
HannesTschofenig 0:796d0f61a05b 47 * \name Structures and functions for X.509 Certificate Signing Requests (CSR)
HannesTschofenig 0:796d0f61a05b 48 * \{
HannesTschofenig 0:796d0f61a05b 49 */
HannesTschofenig 0:796d0f61a05b 50
HannesTschofenig 0:796d0f61a05b 51 /**
HannesTschofenig 0:796d0f61a05b 52 * Certificate Signing Request (CSR) structure.
HannesTschofenig 0:796d0f61a05b 53 */
HannesTschofenig 0:796d0f61a05b 54 typedef struct _x509_csr
HannesTschofenig 0:796d0f61a05b 55 {
HannesTschofenig 0:796d0f61a05b 56 x509_buf raw; /**< The raw CSR data (DER). */
HannesTschofenig 0:796d0f61a05b 57 x509_buf cri; /**< The raw CertificateRequestInfo body (DER). */
HannesTschofenig 0:796d0f61a05b 58
HannesTschofenig 0:796d0f61a05b 59 int version;
HannesTschofenig 0:796d0f61a05b 60
HannesTschofenig 0:796d0f61a05b 61 x509_buf subject_raw; /**< The raw subject data (DER). */
HannesTschofenig 0:796d0f61a05b 62 x509_name subject; /**< The parsed subject data (named information object). */
HannesTschofenig 0:796d0f61a05b 63
HannesTschofenig 0:796d0f61a05b 64 pk_context pk; /**< Container for the public key context. */
HannesTschofenig 0:796d0f61a05b 65
HannesTschofenig 0:796d0f61a05b 66 x509_buf sig_oid;
HannesTschofenig 0:796d0f61a05b 67 x509_buf sig;
HannesTschofenig 0:796d0f61a05b 68 md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. POLARSSL_MD_SHA256 */
HannesTschofenig 0:796d0f61a05b 69 pk_type_t sig_pk /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. POLARSSL_PK_RSA */;
HannesTschofenig 0:796d0f61a05b 70 }
HannesTschofenig 0:796d0f61a05b 71 x509_csr;
HannesTschofenig 0:796d0f61a05b 72
HannesTschofenig 0:796d0f61a05b 73 /**
HannesTschofenig 0:796d0f61a05b 74 * Container for writing a CSR
HannesTschofenig 0:796d0f61a05b 75 */
HannesTschofenig 0:796d0f61a05b 76 typedef struct _x509write_csr
HannesTschofenig 0:796d0f61a05b 77 {
HannesTschofenig 0:796d0f61a05b 78 pk_context *key;
HannesTschofenig 0:796d0f61a05b 79 asn1_named_data *subject;
HannesTschofenig 0:796d0f61a05b 80 md_type_t md_alg;
HannesTschofenig 0:796d0f61a05b 81 asn1_named_data *extensions;
HannesTschofenig 0:796d0f61a05b 82 }
HannesTschofenig 0:796d0f61a05b 83 x509write_csr;
HannesTschofenig 0:796d0f61a05b 84
HannesTschofenig 0:796d0f61a05b 85 #if defined(POLARSSL_X509_CSR_PARSE_C)
HannesTschofenig 0:796d0f61a05b 86 /**
HannesTschofenig 0:796d0f61a05b 87 * \brief Load a Certificate Signing Request (CSR)
HannesTschofenig 0:796d0f61a05b 88 *
HannesTschofenig 0:796d0f61a05b 89 * \param csr CSR context to fill
HannesTschofenig 0:796d0f61a05b 90 * \param buf buffer holding the CRL data
HannesTschofenig 0:796d0f61a05b 91 * \param buflen size of the buffer
HannesTschofenig 0:796d0f61a05b 92 *
HannesTschofenig 0:796d0f61a05b 93 * \return 0 if successful, or a specific X509 or PEM error code
HannesTschofenig 0:796d0f61a05b 94 */
HannesTschofenig 0:796d0f61a05b 95 int x509_csr_parse( x509_csr *csr, const unsigned char *buf, size_t buflen );
HannesTschofenig 0:796d0f61a05b 96
HannesTschofenig 0:796d0f61a05b 97 #if defined(POLARSSL_FS_IO)
HannesTschofenig 0:796d0f61a05b 98 /**
HannesTschofenig 0:796d0f61a05b 99 * \brief Load a Certificate Signing Request (CSR)
HannesTschofenig 0:796d0f61a05b 100 *
HannesTschofenig 0:796d0f61a05b 101 * \param csr CSR context to fill
HannesTschofenig 0:796d0f61a05b 102 * \param path filename to read the CSR from
HannesTschofenig 0:796d0f61a05b 103 *
HannesTschofenig 0:796d0f61a05b 104 * \return 0 if successful, or a specific X509 or PEM error code
HannesTschofenig 0:796d0f61a05b 105 */
HannesTschofenig 0:796d0f61a05b 106 int x509_csr_parse_file( x509_csr *csr, const char *path );
HannesTschofenig 0:796d0f61a05b 107 #endif /* POLARSSL_FS_IO */
HannesTschofenig 0:796d0f61a05b 108
HannesTschofenig 0:796d0f61a05b 109 /**
HannesTschofenig 0:796d0f61a05b 110 * \brief Returns an informational string about the
HannesTschofenig 0:796d0f61a05b 111 * CSR.
HannesTschofenig 0:796d0f61a05b 112 *
HannesTschofenig 0:796d0f61a05b 113 * \param buf Buffer to write to
HannesTschofenig 0:796d0f61a05b 114 * \param size Maximum size of buffer
HannesTschofenig 0:796d0f61a05b 115 * \param prefix A line prefix
HannesTschofenig 0:796d0f61a05b 116 * \param csr The X509 CSR to represent
HannesTschofenig 0:796d0f61a05b 117 *
HannesTschofenig 0:796d0f61a05b 118 * \return The amount of data written to the buffer, or -1 in
HannesTschofenig 0:796d0f61a05b 119 * case of an error.
HannesTschofenig 0:796d0f61a05b 120 */
HannesTschofenig 0:796d0f61a05b 121 int x509_csr_info( char *buf, size_t size, const char *prefix,
HannesTschofenig 0:796d0f61a05b 122 const x509_csr *csr );
HannesTschofenig 0:796d0f61a05b 123
HannesTschofenig 0:796d0f61a05b 124 /**
HannesTschofenig 0:796d0f61a05b 125 * \brief Initialize a CSR
HannesTschofenig 0:796d0f61a05b 126 *
HannesTschofenig 0:796d0f61a05b 127 * \param csr CSR to initialize
HannesTschofenig 0:796d0f61a05b 128 */
HannesTschofenig 0:796d0f61a05b 129 void x509_csr_init( x509_csr *csr );
HannesTschofenig 0:796d0f61a05b 130
HannesTschofenig 0:796d0f61a05b 131 /**
HannesTschofenig 0:796d0f61a05b 132 * \brief Unallocate all CSR data
HannesTschofenig 0:796d0f61a05b 133 *
HannesTschofenig 0:796d0f61a05b 134 * \param csr CSR to free
HannesTschofenig 0:796d0f61a05b 135 */
HannesTschofenig 0:796d0f61a05b 136 void x509_csr_free( x509_csr *csr );
HannesTschofenig 0:796d0f61a05b 137 #endif /* POLARSSL_X509_CSR_PARSE_C */
HannesTschofenig 0:796d0f61a05b 138
HannesTschofenig 0:796d0f61a05b 139 /* \} name */
HannesTschofenig 0:796d0f61a05b 140 /* \} addtogroup x509_module */
HannesTschofenig 0:796d0f61a05b 141
HannesTschofenig 0:796d0f61a05b 142 #if defined(POLARSSL_X509_CSR_WRITE_C)
HannesTschofenig 0:796d0f61a05b 143 /**
HannesTschofenig 0:796d0f61a05b 144 * \brief Initialize a CSR context
HannesTschofenig 0:796d0f61a05b 145 *
HannesTschofenig 0:796d0f61a05b 146 * \param ctx CSR context to initialize
HannesTschofenig 0:796d0f61a05b 147 */
HannesTschofenig 0:796d0f61a05b 148 void x509write_csr_init( x509write_csr *ctx );
HannesTschofenig 0:796d0f61a05b 149
HannesTschofenig 0:796d0f61a05b 150 /**
HannesTschofenig 0:796d0f61a05b 151 * \brief Set the subject name for a CSR
HannesTschofenig 0:796d0f61a05b 152 * Subject names should contain a comma-separated list
HannesTschofenig 0:796d0f61a05b 153 * of OID types and values:
HannesTschofenig 0:796d0f61a05b 154 * e.g. "C=NL,O=Offspark,CN=PolarSSL Server 1"
HannesTschofenig 0:796d0f61a05b 155 *
HannesTschofenig 0:796d0f61a05b 156 * \param ctx CSR context to use
HannesTschofenig 0:796d0f61a05b 157 * \param subject_name subject name to set
HannesTschofenig 0:796d0f61a05b 158 *
HannesTschofenig 0:796d0f61a05b 159 * \return 0 if subject name was parsed successfully, or
HannesTschofenig 0:796d0f61a05b 160 * a specific error code
HannesTschofenig 0:796d0f61a05b 161 */
HannesTschofenig 0:796d0f61a05b 162 int x509write_csr_set_subject_name( x509write_csr *ctx,
HannesTschofenig 0:796d0f61a05b 163 const char *subject_name );
HannesTschofenig 0:796d0f61a05b 164
HannesTschofenig 0:796d0f61a05b 165 /**
HannesTschofenig 0:796d0f61a05b 166 * \brief Set the key for a CSR (public key will be included,
HannesTschofenig 0:796d0f61a05b 167 * private key used to sign the CSR when writing it)
HannesTschofenig 0:796d0f61a05b 168 *
HannesTschofenig 0:796d0f61a05b 169 * \param ctx CSR context to use
HannesTschofenig 0:796d0f61a05b 170 * \param key Asymetric key to include
HannesTschofenig 0:796d0f61a05b 171 */
HannesTschofenig 0:796d0f61a05b 172 void x509write_csr_set_key( x509write_csr *ctx, pk_context *key );
HannesTschofenig 0:796d0f61a05b 173
HannesTschofenig 0:796d0f61a05b 174 /**
HannesTschofenig 0:796d0f61a05b 175 * \brief Set the MD algorithm to use for the signature
HannesTschofenig 0:796d0f61a05b 176 * (e.g. POLARSSL_MD_SHA1)
HannesTschofenig 0:796d0f61a05b 177 *
HannesTschofenig 0:796d0f61a05b 178 * \param ctx CSR context to use
HannesTschofenig 0:796d0f61a05b 179 * \param md_alg MD algorithm to use
HannesTschofenig 0:796d0f61a05b 180 */
HannesTschofenig 0:796d0f61a05b 181 void x509write_csr_set_md_alg( x509write_csr *ctx, md_type_t md_alg );
HannesTschofenig 0:796d0f61a05b 182
HannesTschofenig 0:796d0f61a05b 183 /**
HannesTschofenig 0:796d0f61a05b 184 * \brief Set the Key Usage Extension flags
HannesTschofenig 0:796d0f61a05b 185 * (e.g. KU_DIGITAL_SIGNATURE | KU_KEY_CERT_SIGN)
HannesTschofenig 0:796d0f61a05b 186 *
HannesTschofenig 0:796d0f61a05b 187 * \param ctx CSR context to use
HannesTschofenig 0:796d0f61a05b 188 * \param key_usage key usage flags to set
HannesTschofenig 0:796d0f61a05b 189 *
HannesTschofenig 0:796d0f61a05b 190 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
HannesTschofenig 0:796d0f61a05b 191 */
HannesTschofenig 0:796d0f61a05b 192 int x509write_csr_set_key_usage( x509write_csr *ctx, unsigned char key_usage );
HannesTschofenig 0:796d0f61a05b 193
HannesTschofenig 0:796d0f61a05b 194 /**
HannesTschofenig 0:796d0f61a05b 195 * \brief Set the Netscape Cert Type flags
HannesTschofenig 0:796d0f61a05b 196 * (e.g. NS_CERT_TYPE_SSL_CLIENT | NS_CERT_TYPE_EMAIL)
HannesTschofenig 0:796d0f61a05b 197 *
HannesTschofenig 0:796d0f61a05b 198 * \param ctx CSR context to use
HannesTschofenig 0:796d0f61a05b 199 * \param ns_cert_type Netscape Cert Type flags to set
HannesTschofenig 0:796d0f61a05b 200 *
HannesTschofenig 0:796d0f61a05b 201 * \return 0 if successful, or POLARSSL_ERR_X509WRITE_MALLOC_FAILED
HannesTschofenig 0:796d0f61a05b 202 */
HannesTschofenig 0:796d0f61a05b 203 int x509write_csr_set_ns_cert_type( x509write_csr *ctx,
HannesTschofenig 0:796d0f61a05b 204 unsigned char ns_cert_type );
HannesTschofenig 0:796d0f61a05b 205
HannesTschofenig 0:796d0f61a05b 206 /**
HannesTschofenig 0:796d0f61a05b 207 * \brief Generic function to add to or replace an extension in the
HannesTschofenig 0:796d0f61a05b 208 * CSR
HannesTschofenig 0:796d0f61a05b 209 *
HannesTschofenig 0:796d0f61a05b 210 * \param ctx CSR context to use
HannesTschofenig 0:796d0f61a05b 211 * \param oid OID of the extension
HannesTschofenig 0:796d0f61a05b 212 * \param oid_len length of the OID
HannesTschofenig 0:796d0f61a05b 213 * \param val value of the extension OCTET STRING
HannesTschofenig 0:796d0f61a05b 214 * \param val_len length of the value data
HannesTschofenig 0:796d0f61a05b 215 *
HannesTschofenig 0:796d0f61a05b 216 * \return 0 if successful, or a POLARSSL_ERR_X509WRITE_MALLOC_FAILED
HannesTschofenig 0:796d0f61a05b 217 */
HannesTschofenig 0:796d0f61a05b 218 int x509write_csr_set_extension( x509write_csr *ctx,
HannesTschofenig 0:796d0f61a05b 219 const char *oid, size_t oid_len,
HannesTschofenig 0:796d0f61a05b 220 const unsigned char *val, size_t val_len );
HannesTschofenig 0:796d0f61a05b 221
HannesTschofenig 0:796d0f61a05b 222 /**
HannesTschofenig 0:796d0f61a05b 223 * \brief Free the contents of a CSR context
HannesTschofenig 0:796d0f61a05b 224 *
HannesTschofenig 0:796d0f61a05b 225 * \param ctx CSR context to free
HannesTschofenig 0:796d0f61a05b 226 */
HannesTschofenig 0:796d0f61a05b 227 void x509write_csr_free( x509write_csr *ctx );
HannesTschofenig 0:796d0f61a05b 228
HannesTschofenig 0:796d0f61a05b 229 /**
HannesTschofenig 0:796d0f61a05b 230 * \brief Write a CSR (Certificate Signing Request) to a
HannesTschofenig 0:796d0f61a05b 231 * DER structure
HannesTschofenig 0:796d0f61a05b 232 * Note: data is written at the end of the buffer! Use the
HannesTschofenig 0:796d0f61a05b 233 * return value to determine where you should start
HannesTschofenig 0:796d0f61a05b 234 * using the buffer
HannesTschofenig 0:796d0f61a05b 235 *
HannesTschofenig 0:796d0f61a05b 236 * \param ctx CSR to write away
HannesTschofenig 0:796d0f61a05b 237 * \param buf buffer to write to
HannesTschofenig 0:796d0f61a05b 238 * \param size size of the buffer
HannesTschofenig 0:796d0f61a05b 239 * \param f_rng RNG function (for signature, see note)
HannesTschofenig 0:796d0f61a05b 240 * \param p_rng RNG parameter
HannesTschofenig 0:796d0f61a05b 241 *
HannesTschofenig 0:796d0f61a05b 242 * \return length of data written if successful, or a specific
HannesTschofenig 0:796d0f61a05b 243 * error code
HannesTschofenig 0:796d0f61a05b 244 *
HannesTschofenig 0:796d0f61a05b 245 * \note f_rng may be NULL if RSA is used for signature and the
HannesTschofenig 0:796d0f61a05b 246 * signature is made offline (otherwise f_rng is desirable
HannesTschofenig 0:796d0f61a05b 247 * for countermeasures against timing attacks).
HannesTschofenig 0:796d0f61a05b 248 * ECDSA signatures always require a non-NULL f_rng.
HannesTschofenig 0:796d0f61a05b 249 */
HannesTschofenig 0:796d0f61a05b 250 int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
HannesTschofenig 0:796d0f61a05b 251 int (*f_rng)(void *, unsigned char *, size_t),
HannesTschofenig 0:796d0f61a05b 252 void *p_rng );
HannesTschofenig 0:796d0f61a05b 253
HannesTschofenig 0:796d0f61a05b 254 #if defined(POLARSSL_PEM_WRITE_C)
HannesTschofenig 0:796d0f61a05b 255 /**
HannesTschofenig 0:796d0f61a05b 256 * \brief Write a CSR (Certificate Signing Request) to a
HannesTschofenig 0:796d0f61a05b 257 * PEM string
HannesTschofenig 0:796d0f61a05b 258 *
HannesTschofenig 0:796d0f61a05b 259 * \param ctx CSR to write away
HannesTschofenig 0:796d0f61a05b 260 * \param buf buffer to write to
HannesTschofenig 0:796d0f61a05b 261 * \param size size of the buffer
HannesTschofenig 0:796d0f61a05b 262 * \param f_rng RNG function (for signature, see note)
HannesTschofenig 0:796d0f61a05b 263 * \param p_rng RNG parameter
HannesTschofenig 0:796d0f61a05b 264 *
HannesTschofenig 0:796d0f61a05b 265 * \return 0 successful, or a specific error code
HannesTschofenig 0:796d0f61a05b 266 *
HannesTschofenig 0:796d0f61a05b 267 * \note f_rng may be NULL if RSA is used for signature and the
HannesTschofenig 0:796d0f61a05b 268 * signature is made offline (otherwise f_rng is desirable
HannesTschofenig 0:796d0f61a05b 269 * for couermeasures against timing attacks).
HannesTschofenig 0:796d0f61a05b 270 * ECDSA signatures always require a non-NULL f_rng.
HannesTschofenig 0:796d0f61a05b 271 */
HannesTschofenig 0:796d0f61a05b 272 int x509write_csr_pem( x509write_csr *ctx, unsigned char *buf, size_t size,
HannesTschofenig 0:796d0f61a05b 273 int (*f_rng)(void *, unsigned char *, size_t),
HannesTschofenig 0:796d0f61a05b 274 void *p_rng );
HannesTschofenig 0:796d0f61a05b 275 #endif /* POLARSSL_PEM_WRITE_C */
HannesTschofenig 0:796d0f61a05b 276 #endif /* POLARSSL_X509_CSR_WRITE_C */
HannesTschofenig 0:796d0f61a05b 277
HannesTschofenig 0:796d0f61a05b 278 #ifdef __cplusplus
HannesTschofenig 0:796d0f61a05b 279 }
HannesTschofenig 0:796d0f61a05b 280 #endif
HannesTschofenig 0:796d0f61a05b 281
HannesTschofenig 0:796d0f61a05b 282 #endif /* x509_csr.h */
HannesTschofenig 0:796d0f61a05b 283
HannesTschofenig 0:796d0f61a05b 284