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 compat-1.2.h
HannesTschofenig 0:796d0f61a05b 3 *
HannesTschofenig 0:796d0f61a05b 4 * \brief Backwards compatibility header for PolarSSL-1.2 from PolarSSL-1.3
HannesTschofenig 0:796d0f61a05b 5 *
HannesTschofenig 0:796d0f61a05b 6 * Copyright (C) 2006-2013, 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_COMPAT_1_2_H
HannesTschofenig 0:796d0f61a05b 28 #define POLARSSL_COMPAT_1_2_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 // Comment out to disable prototype change warnings
HannesTschofenig 0:796d0f61a05b 37 #define SHOW_PROTOTYPE_CHANGE_WARNINGS
HannesTschofenig 0:796d0f61a05b 38
HannesTschofenig 0:796d0f61a05b 39 #if defined(_MSC_VER) && !defined(inline)
HannesTschofenig 0:796d0f61a05b 40 #define inline _inline
HannesTschofenig 0:796d0f61a05b 41 #else
HannesTschofenig 0:796d0f61a05b 42 #if defined(__ARMCC_VERSION) && !defined(inline)
HannesTschofenig 0:796d0f61a05b 43 #define inline __inline
HannesTschofenig 0:796d0f61a05b 44 #endif /* __ARMCC_VERSION */
HannesTschofenig 0:796d0f61a05b 45 #endif /* _MSC_VER */
HannesTschofenig 0:796d0f61a05b 46
HannesTschofenig 0:796d0f61a05b 47 #if defined(_MSC_VER)
HannesTschofenig 0:796d0f61a05b 48 // MSVC does not support #warning
HannesTschofenig 0:796d0f61a05b 49 #undef SHOW_PROTOTYPE_CHANGE_WARNINGS
HannesTschofenig 0:796d0f61a05b 50 #endif
HannesTschofenig 0:796d0f61a05b 51
HannesTschofenig 0:796d0f61a05b 52 #if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
HannesTschofenig 0:796d0f61a05b 53 #warning "You can disable these warnings by commenting SHOW_PROTOTYPE_CHANGE_WARNINGS in compat-1.2.h"
HannesTschofenig 0:796d0f61a05b 54 #endif
HannesTschofenig 0:796d0f61a05b 55
HannesTschofenig 0:796d0f61a05b 56 #if defined(POLARSSL_SHA256_C)
HannesTschofenig 0:796d0f61a05b 57 #define POLARSSL_SHA2_C
HannesTschofenig 0:796d0f61a05b 58 #include "sha256.h"
HannesTschofenig 0:796d0f61a05b 59
HannesTschofenig 0:796d0f61a05b 60 /*
HannesTschofenig 0:796d0f61a05b 61 * SHA-2 -> SHA-256
HannesTschofenig 0:796d0f61a05b 62 */
HannesTschofenig 0:796d0f61a05b 63 typedef sha256_context sha2_context;
HannesTschofenig 0:796d0f61a05b 64
HannesTschofenig 0:796d0f61a05b 65 static inline void sha2_starts( sha256_context *ctx, int is224 ) {
HannesTschofenig 0:796d0f61a05b 66 sha256_starts( ctx, is224 );
HannesTschofenig 0:796d0f61a05b 67 }
HannesTschofenig 0:796d0f61a05b 68 static inline void sha2_update( sha256_context *ctx, const unsigned char *input,
HannesTschofenig 0:796d0f61a05b 69 size_t ilen ) {
HannesTschofenig 0:796d0f61a05b 70 sha256_update( ctx, input, ilen );
HannesTschofenig 0:796d0f61a05b 71 }
HannesTschofenig 0:796d0f61a05b 72 static inline void sha2_finish( sha256_context *ctx, unsigned char output[32] ) {
HannesTschofenig 0:796d0f61a05b 73 sha256_finish( ctx, output );
HannesTschofenig 0:796d0f61a05b 74 }
HannesTschofenig 0:796d0f61a05b 75 static inline int sha2_file( const char *path, unsigned char output[32], int is224 ) {
HannesTschofenig 0:796d0f61a05b 76 return sha256_file( path, output, is224 );
HannesTschofenig 0:796d0f61a05b 77 }
HannesTschofenig 0:796d0f61a05b 78 static inline void sha2( const unsigned char *input, size_t ilen,
HannesTschofenig 0:796d0f61a05b 79 unsigned char output[32], int is224 ) {
HannesTschofenig 0:796d0f61a05b 80 sha256( input, ilen, output, is224 );
HannesTschofenig 0:796d0f61a05b 81 }
HannesTschofenig 0:796d0f61a05b 82 static inline void sha2_hmac_starts( sha256_context *ctx, const unsigned char *key,
HannesTschofenig 0:796d0f61a05b 83 size_t keylen, int is224 ) {
HannesTschofenig 0:796d0f61a05b 84 sha256_hmac_starts( ctx, key, keylen, is224 );
HannesTschofenig 0:796d0f61a05b 85 }
HannesTschofenig 0:796d0f61a05b 86 static inline void sha2_hmac_update( sha256_context *ctx, const unsigned char *input, size_t ilen ) {
HannesTschofenig 0:796d0f61a05b 87 sha256_hmac_update( ctx, input, ilen );
HannesTschofenig 0:796d0f61a05b 88 }
HannesTschofenig 0:796d0f61a05b 89 static inline void sha2_hmac_finish( sha256_context *ctx, unsigned char output[32] ) {
HannesTschofenig 0:796d0f61a05b 90 sha256_hmac_finish( ctx, output );
HannesTschofenig 0:796d0f61a05b 91 }
HannesTschofenig 0:796d0f61a05b 92 static inline void sha2_hmac_reset( sha256_context *ctx ) {
HannesTschofenig 0:796d0f61a05b 93 sha256_hmac_reset( ctx );
HannesTschofenig 0:796d0f61a05b 94 }
HannesTschofenig 0:796d0f61a05b 95 static inline void sha2_hmac( const unsigned char *key, size_t keylen,
HannesTschofenig 0:796d0f61a05b 96 const unsigned char *input, size_t ilen,
HannesTschofenig 0:796d0f61a05b 97 unsigned char output[32], int is224 ) {
HannesTschofenig 0:796d0f61a05b 98 sha256_hmac( key, keylen, input, ilen, output, is224 );
HannesTschofenig 0:796d0f61a05b 99 }
HannesTschofenig 0:796d0f61a05b 100 static inline int sha2_self_test( int verbose ) {
HannesTschofenig 0:796d0f61a05b 101 return sha256_self_test( verbose );
HannesTschofenig 0:796d0f61a05b 102 }
HannesTschofenig 0:796d0f61a05b 103 #endif /* POLARSSL_SHA256_C */
HannesTschofenig 0:796d0f61a05b 104
HannesTschofenig 0:796d0f61a05b 105 #if defined(POLARSSL_SHA512_C)
HannesTschofenig 0:796d0f61a05b 106 #define POLARSSL_SHA4_C
HannesTschofenig 0:796d0f61a05b 107 #include "sha512.h"
HannesTschofenig 0:796d0f61a05b 108
HannesTschofenig 0:796d0f61a05b 109 /*
HannesTschofenig 0:796d0f61a05b 110 * SHA-4 -> SHA-512
HannesTschofenig 0:796d0f61a05b 111 */
HannesTschofenig 0:796d0f61a05b 112 typedef sha512_context sha4_context;
HannesTschofenig 0:796d0f61a05b 113
HannesTschofenig 0:796d0f61a05b 114 static inline void sha4_starts( sha512_context *ctx, int is384 ) {
HannesTschofenig 0:796d0f61a05b 115 sha512_starts( ctx, is384 );
HannesTschofenig 0:796d0f61a05b 116 }
HannesTschofenig 0:796d0f61a05b 117 static inline void sha4_update( sha512_context *ctx, const unsigned char *input,
HannesTschofenig 0:796d0f61a05b 118 size_t ilen ) {
HannesTschofenig 0:796d0f61a05b 119 sha512_update( ctx, input, ilen );
HannesTschofenig 0:796d0f61a05b 120 }
HannesTschofenig 0:796d0f61a05b 121 static inline void sha4_finish( sha512_context *ctx, unsigned char output[64] ) {
HannesTschofenig 0:796d0f61a05b 122 sha512_finish( ctx, output );
HannesTschofenig 0:796d0f61a05b 123 }
HannesTschofenig 0:796d0f61a05b 124 static inline int sha4_file( const char *path, unsigned char output[64], int is384 ) {
HannesTschofenig 0:796d0f61a05b 125 return sha512_file( path, output, is384 );
HannesTschofenig 0:796d0f61a05b 126 }
HannesTschofenig 0:796d0f61a05b 127 static inline void sha4( const unsigned char *input, size_t ilen,
HannesTschofenig 0:796d0f61a05b 128 unsigned char output[32], int is384 ) {
HannesTschofenig 0:796d0f61a05b 129 sha512( input, ilen, output, is384 );
HannesTschofenig 0:796d0f61a05b 130 }
HannesTschofenig 0:796d0f61a05b 131 static inline void sha4_hmac_starts( sha512_context *ctx, const unsigned char *key,
HannesTschofenig 0:796d0f61a05b 132 size_t keylen, int is384 ) {
HannesTschofenig 0:796d0f61a05b 133 sha512_hmac_starts( ctx, key, keylen, is384 );
HannesTschofenig 0:796d0f61a05b 134 }
HannesTschofenig 0:796d0f61a05b 135 static inline void sha4_hmac_update( sha512_context *ctx, const unsigned char *input, size_t ilen ) {
HannesTschofenig 0:796d0f61a05b 136 sha512_hmac_update( ctx, input, ilen );
HannesTschofenig 0:796d0f61a05b 137 }
HannesTschofenig 0:796d0f61a05b 138 static inline void sha4_hmac_finish( sha512_context *ctx, unsigned char output[64] ) {
HannesTschofenig 0:796d0f61a05b 139 sha512_hmac_finish( ctx, output );
HannesTschofenig 0:796d0f61a05b 140 }
HannesTschofenig 0:796d0f61a05b 141 static inline void sha4_hmac_reset( sha512_context *ctx ) {
HannesTschofenig 0:796d0f61a05b 142 sha512_hmac_reset( ctx );
HannesTschofenig 0:796d0f61a05b 143 }
HannesTschofenig 0:796d0f61a05b 144 static inline void sha4_hmac( const unsigned char *key, size_t keylen,
HannesTschofenig 0:796d0f61a05b 145 const unsigned char *input, size_t ilen,
HannesTschofenig 0:796d0f61a05b 146 unsigned char output[64], int is384 ) {
HannesTschofenig 0:796d0f61a05b 147 sha512_hmac( key, keylen, input, ilen, output, is384 );
HannesTschofenig 0:796d0f61a05b 148 }
HannesTschofenig 0:796d0f61a05b 149 static inline int sha4_self_test( int verbose ) {
HannesTschofenig 0:796d0f61a05b 150 return sha512_self_test( verbose );
HannesTschofenig 0:796d0f61a05b 151 }
HannesTschofenig 0:796d0f61a05b 152 #endif /* POLARSSL_SHA512_C */
HannesTschofenig 0:796d0f61a05b 153
HannesTschofenig 0:796d0f61a05b 154 #if defined(POLARSSL_CIPHER_C)
HannesTschofenig 0:796d0f61a05b 155 #if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
HannesTschofenig 0:796d0f61a05b 156 #warning "cipher_reset() prototype changed. Manual change required if used"
HannesTschofenig 0:796d0f61a05b 157 #endif
HannesTschofenig 0:796d0f61a05b 158 #endif
HannesTschofenig 0:796d0f61a05b 159
HannesTschofenig 0:796d0f61a05b 160 #if defined(POLARSSL_RSA_C)
HannesTschofenig 0:796d0f61a05b 161 #define SIG_RSA_RAW POLARSSL_MD_NONE
HannesTschofenig 0:796d0f61a05b 162 #define SIG_RSA_MD2 POLARSSL_MD_MD2
HannesTschofenig 0:796d0f61a05b 163 #define SIG_RSA_MD4 POLARSSL_MD_MD4
HannesTschofenig 0:796d0f61a05b 164 #define SIG_RSA_MD5 POLARSSL_MD_MD5
HannesTschofenig 0:796d0f61a05b 165 #define SIG_RSA_SHA1 POLARSSL_MD_SHA1
HannesTschofenig 0:796d0f61a05b 166 #define SIG_RSA_SHA224 POLARSSL_MD_SHA224
HannesTschofenig 0:796d0f61a05b 167 #define SIG_RSA_SHA256 POLARSSL_MD_SHA256
HannesTschofenig 0:796d0f61a05b 168 #define SIG_RSA_SHA384 POLARSSL_MD_SHA384
HannesTschofenig 0:796d0f61a05b 169 #define SIG_RSA_SHA512 POLARSSL_MD_SHA512
HannesTschofenig 0:796d0f61a05b 170 #if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
HannesTschofenig 0:796d0f61a05b 171 #warning "rsa_pkcs1_verify() prototype changed. Manual change required if used"
HannesTschofenig 0:796d0f61a05b 172 #warning "rsa_pkcs1_decrypt() prototype changed. Manual change required if used"
HannesTschofenig 0:796d0f61a05b 173 #endif
HannesTschofenig 0:796d0f61a05b 174 #endif /* POLARSSL_RSA_C */
HannesTschofenig 0:796d0f61a05b 175
HannesTschofenig 0:796d0f61a05b 176 #if defined(POLARSSL_DHM_C)
HannesTschofenig 0:796d0f61a05b 177 #if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
HannesTschofenig 0:796d0f61a05b 178 #warning "dhm_calc_secret() prototype changed. Manual change required if used"
HannesTschofenig 0:796d0f61a05b 179 #endif
HannesTschofenig 0:796d0f61a05b 180 #endif
HannesTschofenig 0:796d0f61a05b 181
HannesTschofenig 0:796d0f61a05b 182 #if defined(POLARSSL_GCM_C)
HannesTschofenig 0:796d0f61a05b 183 #if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
HannesTschofenig 0:796d0f61a05b 184 #warning "gcm_init() prototype changed. Manual change required if used"
HannesTschofenig 0:796d0f61a05b 185 #endif
HannesTschofenig 0:796d0f61a05b 186 #endif
HannesTschofenig 0:796d0f61a05b 187
HannesTschofenig 0:796d0f61a05b 188 #if defined(POLARSSL_SSL_CLI_C)
HannesTschofenig 0:796d0f61a05b 189 #if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
HannesTschofenig 0:796d0f61a05b 190 #warning "ssl_set_own_cert() prototype changed. Change to ssl_set_own_cert_rsa(). Manual change required if used"
HannesTschofenig 0:796d0f61a05b 191 #endif
HannesTschofenig 0:796d0f61a05b 192 #endif
HannesTschofenig 0:796d0f61a05b 193
HannesTschofenig 0:796d0f61a05b 194 #if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
HannesTschofenig 0:796d0f61a05b 195 #include "x509.h"
HannesTschofenig 0:796d0f61a05b 196
HannesTschofenig 0:796d0f61a05b 197 #define POLARSSL_ERR_X509_CERT_INVALID_FORMAT POLARSSL_ERR_X509_INVALID_FORMAT
HannesTschofenig 0:796d0f61a05b 198 #define POLARSSL_ERR_X509_CERT_INVALID_VERSION POLARSSL_ERR_X509_INVALID_VERSION
HannesTschofenig 0:796d0f61a05b 199 #define POLARSSL_ERR_X509_CERT_INVALID_ALG POLARSSL_ERR_X509_INVALID_ALG
HannesTschofenig 0:796d0f61a05b 200 #define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG POLARSSL_ERR_X509_UNKNOWN_SIG_ALG
HannesTschofenig 0:796d0f61a05b 201 #define POLARSSL_ERR_X509_CERT_INVALID_NAME POLARSSL_ERR_X509_INVALID_NAME
HannesTschofenig 0:796d0f61a05b 202 #define POLARSSL_ERR_X509_CERT_INVALID_DATE POLARSSL_ERR_X509_INVALID_DATE
HannesTschofenig 0:796d0f61a05b 203 #define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS POLARSSL_ERR_X509_INVALID_EXTENSIONS
HannesTschofenig 0:796d0f61a05b 204 #define POLARSSL_ERR_X509_CERT_SIG_MISMATCH POLARSSL_ERR_X509_SIG_MISMATCH
HannesTschofenig 0:796d0f61a05b 205 #define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE POLARSSL_ERR_X509_INVALID_SIGNATURE
HannesTschofenig 0:796d0f61a05b 206 #define POLARSSL_ERR_X509_CERT_INVALID_SERIAL POLARSSL_ERR_X509_INVALID_SERIAL
HannesTschofenig 0:796d0f61a05b 207 #define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION POLARSSL_ERR_X509_UNKNOWN_VERSION
HannesTschofenig 0:796d0f61a05b 208
HannesTschofenig 0:796d0f61a05b 209 static inline int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial ) {
HannesTschofenig 0:796d0f61a05b 210 return x509_serial_gets( buf, size, serial );
HannesTschofenig 0:796d0f61a05b 211 }
HannesTschofenig 0:796d0f61a05b 212 static inline int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn ) {
HannesTschofenig 0:796d0f61a05b 213 return x509_dn_gets( buf, size, dn );
HannesTschofenig 0:796d0f61a05b 214 }
HannesTschofenig 0:796d0f61a05b 215 static inline int x509parse_time_expired( const x509_time *time ) {
HannesTschofenig 0:796d0f61a05b 216 return x509_time_expired( time );
HannesTschofenig 0:796d0f61a05b 217 }
HannesTschofenig 0:796d0f61a05b 218 #endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
HannesTschofenig 0:796d0f61a05b 219
HannesTschofenig 0:796d0f61a05b 220 #if defined(POLARSSL_X509_CRT_PARSE_C)
HannesTschofenig 0:796d0f61a05b 221 #define POLARSSL_X509_PARSE_C
HannesTschofenig 0:796d0f61a05b 222 #include "x509_crt.h"
HannesTschofenig 0:796d0f61a05b 223 typedef x509_crt x509_cert;
HannesTschofenig 0:796d0f61a05b 224
HannesTschofenig 0:796d0f61a05b 225 static inline int x509parse_crt_der( x509_cert *chain, const unsigned char *buf,
HannesTschofenig 0:796d0f61a05b 226 size_t buflen ) {
HannesTschofenig 0:796d0f61a05b 227 return x509_crt_parse_der( chain, buf, buflen );
HannesTschofenig 0:796d0f61a05b 228 }
HannesTschofenig 0:796d0f61a05b 229 static inline int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen ) {
HannesTschofenig 0:796d0f61a05b 230 return x509_crt_parse( chain, buf, buflen );
HannesTschofenig 0:796d0f61a05b 231 }
HannesTschofenig 0:796d0f61a05b 232 static inline int x509parse_crtfile( x509_cert *chain, const char *path ) {
HannesTschofenig 0:796d0f61a05b 233 return x509_crt_parse_file( chain, path );
HannesTschofenig 0:796d0f61a05b 234 }
HannesTschofenig 0:796d0f61a05b 235 static inline int x509parse_crtpath( x509_cert *chain, const char *path ) {
HannesTschofenig 0:796d0f61a05b 236 return x509_crt_parse_path( chain, path );
HannesTschofenig 0:796d0f61a05b 237 }
HannesTschofenig 0:796d0f61a05b 238 static inline int x509parse_cert_info( char *buf, size_t size, const char *prefix,
HannesTschofenig 0:796d0f61a05b 239 const x509_cert *crt ) {
HannesTschofenig 0:796d0f61a05b 240 return x509_crt_info( buf, size, prefix, crt );
HannesTschofenig 0:796d0f61a05b 241 }
HannesTschofenig 0:796d0f61a05b 242 static inline int x509parse_verify( x509_cert *crt, x509_cert *trust_ca,
HannesTschofenig 0:796d0f61a05b 243 x509_crl *ca_crl, const char *cn, int *flags,
HannesTschofenig 0:796d0f61a05b 244 int (*f_vrfy)(void *, x509_cert *, int, int *),
HannesTschofenig 0:796d0f61a05b 245 void *p_vrfy ) {
HannesTschofenig 0:796d0f61a05b 246 return x509_crt_verify( crt, trust_ca, ca_crl, cn, flags, f_vrfy, p_vrfy );
HannesTschofenig 0:796d0f61a05b 247 }
HannesTschofenig 0:796d0f61a05b 248 static inline int x509parse_revoked( const x509_cert *crt, const x509_crl *crl ) {
HannesTschofenig 0:796d0f61a05b 249 return x509_crt_revoked( crt, crl );
HannesTschofenig 0:796d0f61a05b 250 }
HannesTschofenig 0:796d0f61a05b 251 static inline void x509_free( x509_cert *crt ) {
HannesTschofenig 0:796d0f61a05b 252 x509_crt_free( crt );
HannesTschofenig 0:796d0f61a05b 253 }
HannesTschofenig 0:796d0f61a05b 254 #endif /* POLARSSL_X509_CRT_PARSE_C */
HannesTschofenig 0:796d0f61a05b 255
HannesTschofenig 0:796d0f61a05b 256 #if defined(POLARSSL_X509_CRL_PARSE_C)
HannesTschofenig 0:796d0f61a05b 257 #define POLARSSL_X509_PARSE_C
HannesTschofenig 0:796d0f61a05b 258 #include "x509_crl.h"
HannesTschofenig 0:796d0f61a05b 259 static inline int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen ) {
HannesTschofenig 0:796d0f61a05b 260 return x509_crl_parse( chain, buf, buflen );
HannesTschofenig 0:796d0f61a05b 261 }
HannesTschofenig 0:796d0f61a05b 262 static inline int x509parse_crlfile( x509_crl *chain, const char *path ) {
HannesTschofenig 0:796d0f61a05b 263 return x509_crl_parse_file( chain, path );
HannesTschofenig 0:796d0f61a05b 264 }
HannesTschofenig 0:796d0f61a05b 265 static inline int x509parse_crl_info( char *buf, size_t size, const char *prefix,
HannesTschofenig 0:796d0f61a05b 266 const x509_crl *crl ) {
HannesTschofenig 0:796d0f61a05b 267 return x509_crl_info( buf, size, prefix, crl );
HannesTschofenig 0:796d0f61a05b 268 }
HannesTschofenig 0:796d0f61a05b 269 #endif /* POLARSSL_X509_CRL_PARSE_C */
HannesTschofenig 0:796d0f61a05b 270
HannesTschofenig 0:796d0f61a05b 271 #if defined(POLARSSL_X509_CSR_PARSE_C)
HannesTschofenig 0:796d0f61a05b 272 #define POLARSSL_X509_PARSE_C
HannesTschofenig 0:796d0f61a05b 273 #include "x509_csr.h"
HannesTschofenig 0:796d0f61a05b 274 static inline int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen ) {
HannesTschofenig 0:796d0f61a05b 275 return x509_csr_parse( csr, buf, buflen );
HannesTschofenig 0:796d0f61a05b 276 }
HannesTschofenig 0:796d0f61a05b 277 static inline int x509parse_csrfile( x509_csr *csr, const char *path ) {
HannesTschofenig 0:796d0f61a05b 278 return x509_csr_parse_file( csr, path );
HannesTschofenig 0:796d0f61a05b 279 }
HannesTschofenig 0:796d0f61a05b 280 static inline int x509parse_csr_info( char *buf, size_t size, const char *prefix,
HannesTschofenig 0:796d0f61a05b 281 const x509_csr *csr ) {
HannesTschofenig 0:796d0f61a05b 282 return x509_csr_info( buf, size, prefix, csr );
HannesTschofenig 0:796d0f61a05b 283 }
HannesTschofenig 0:796d0f61a05b 284 #endif /* POLARSSL_X509_CSR_PARSE_C */
HannesTschofenig 0:796d0f61a05b 285
HannesTschofenig 0:796d0f61a05b 286 #if defined(POLARSSL_SSL_TLS_C)
HannesTschofenig 0:796d0f61a05b 287 #include "ssl_ciphersuites.h"
HannesTschofenig 0:796d0f61a05b 288
HannesTschofenig 0:796d0f61a05b 289 #define ssl_default_ciphersuites ssl_list_ciphersuites()
HannesTschofenig 0:796d0f61a05b 290 #endif
HannesTschofenig 0:796d0f61a05b 291
HannesTschofenig 0:796d0f61a05b 292 #if defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_RSA_C)
HannesTschofenig 0:796d0f61a05b 293 #include "rsa.h"
HannesTschofenig 0:796d0f61a05b 294 #include "pk.h"
HannesTschofenig 0:796d0f61a05b 295
HannesTschofenig 0:796d0f61a05b 296 #define POLARSSL_ERR_X509_PASSWORD_MISMATCH POLARSSL_ERR_PK_PASSWORD_MISMATCH
HannesTschofenig 0:796d0f61a05b 297 #define POLARSSL_ERR_X509_KEY_INVALID_FORMAT POLARSSL_ERR_PK_KEY_INVALID_FORMAT
HannesTschofenig 0:796d0f61a05b 298 #define POLARSSL_ERR_X509_UNKNOWN_PK_ALG POLARSSL_ERR_PK_UNKNOWN_PK_ALG
HannesTschofenig 0:796d0f61a05b 299 #define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY POLARSSL_ERR_PK_INVALID_PUBKEY
HannesTschofenig 0:796d0f61a05b 300
HannesTschofenig 0:796d0f61a05b 301 #if defined(POLARSSL_FS_IO)
HannesTschofenig 0:796d0f61a05b 302 static inline int x509parse_keyfile( rsa_context *rsa, const char *path,
HannesTschofenig 0:796d0f61a05b 303 const char *pwd ) {
HannesTschofenig 0:796d0f61a05b 304 int ret;
HannesTschofenig 0:796d0f61a05b 305 pk_context pk;
HannesTschofenig 0:796d0f61a05b 306 pk_init( &pk );
HannesTschofenig 0:796d0f61a05b 307 ret = pk_parse_keyfile( &pk, path, pwd );
HannesTschofenig 0:796d0f61a05b 308 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
HannesTschofenig 0:796d0f61a05b 309 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
HannesTschofenig 0:796d0f61a05b 310 if( ret == 0 )
HannesTschofenig 0:796d0f61a05b 311 rsa_copy( rsa, pk_rsa( pk ) );
HannesTschofenig 0:796d0f61a05b 312 else
HannesTschofenig 0:796d0f61a05b 313 rsa_free( rsa );
HannesTschofenig 0:796d0f61a05b 314 pk_free( &pk );
HannesTschofenig 0:796d0f61a05b 315 return( ret );
HannesTschofenig 0:796d0f61a05b 316 }
HannesTschofenig 0:796d0f61a05b 317 static inline int x509parse_public_keyfile( rsa_context *rsa, const char *path ) {
HannesTschofenig 0:796d0f61a05b 318 int ret;
HannesTschofenig 0:796d0f61a05b 319 pk_context pk;
HannesTschofenig 0:796d0f61a05b 320 pk_init( &pk );
HannesTschofenig 0:796d0f61a05b 321 ret = pk_parse_public_keyfile( &pk, path );
HannesTschofenig 0:796d0f61a05b 322 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
HannesTschofenig 0:796d0f61a05b 323 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
HannesTschofenig 0:796d0f61a05b 324 if( ret == 0 )
HannesTschofenig 0:796d0f61a05b 325 rsa_copy( rsa, pk_rsa( pk ) );
HannesTschofenig 0:796d0f61a05b 326 else
HannesTschofenig 0:796d0f61a05b 327 rsa_free( rsa );
HannesTschofenig 0:796d0f61a05b 328 pk_free( &pk );
HannesTschofenig 0:796d0f61a05b 329 return( ret );
HannesTschofenig 0:796d0f61a05b 330 }
HannesTschofenig 0:796d0f61a05b 331 #endif /* POLARSSL_FS_IO */
HannesTschofenig 0:796d0f61a05b 332
HannesTschofenig 0:796d0f61a05b 333 static inline int x509parse_key( rsa_context *rsa, const unsigned char *key,
HannesTschofenig 0:796d0f61a05b 334 size_t keylen,
HannesTschofenig 0:796d0f61a05b 335 const unsigned char *pwd, size_t pwdlen ) {
HannesTschofenig 0:796d0f61a05b 336 int ret;
HannesTschofenig 0:796d0f61a05b 337 pk_context pk;
HannesTschofenig 0:796d0f61a05b 338 pk_init( &pk );
HannesTschofenig 0:796d0f61a05b 339 ret = pk_parse_key( &pk, key, keylen, pwd, pwdlen );
HannesTschofenig 0:796d0f61a05b 340 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
HannesTschofenig 0:796d0f61a05b 341 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
HannesTschofenig 0:796d0f61a05b 342 if( ret == 0 )
HannesTschofenig 0:796d0f61a05b 343 rsa_copy( rsa, pk_rsa( pk ) );
HannesTschofenig 0:796d0f61a05b 344 else
HannesTschofenig 0:796d0f61a05b 345 rsa_free( rsa );
HannesTschofenig 0:796d0f61a05b 346 pk_free( &pk );
HannesTschofenig 0:796d0f61a05b 347 return( ret );
HannesTschofenig 0:796d0f61a05b 348 }
HannesTschofenig 0:796d0f61a05b 349
HannesTschofenig 0:796d0f61a05b 350 static inline int x509parse_public_key( rsa_context *rsa,
HannesTschofenig 0:796d0f61a05b 351 const unsigned char *key, size_t keylen )
HannesTschofenig 0:796d0f61a05b 352 {
HannesTschofenig 0:796d0f61a05b 353 int ret;
HannesTschofenig 0:796d0f61a05b 354 pk_context pk;
HannesTschofenig 0:796d0f61a05b 355 pk_init( &pk );
HannesTschofenig 0:796d0f61a05b 356 ret = pk_parse_public_key( &pk, key, keylen );
HannesTschofenig 0:796d0f61a05b 357 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
HannesTschofenig 0:796d0f61a05b 358 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
HannesTschofenig 0:796d0f61a05b 359 if( ret == 0 )
HannesTschofenig 0:796d0f61a05b 360 rsa_copy( rsa, pk_rsa( pk ) );
HannesTschofenig 0:796d0f61a05b 361 else
HannesTschofenig 0:796d0f61a05b 362 rsa_free( rsa );
HannesTschofenig 0:796d0f61a05b 363 pk_free( &pk );
HannesTschofenig 0:796d0f61a05b 364 return( ret );
HannesTschofenig 0:796d0f61a05b 365 }
HannesTschofenig 0:796d0f61a05b 366 #endif /* POLARSSL_PK_PARSE_C && POLARSSL_RSA_C */
HannesTschofenig 0:796d0f61a05b 367
HannesTschofenig 0:796d0f61a05b 368 #if defined(POLARSSL_PK_WRITE_C) && defined(POLARSSL_RSA_C)
HannesTschofenig 0:796d0f61a05b 369 #include "pk.h"
HannesTschofenig 0:796d0f61a05b 370 static inline int x509_write_pubkey_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
HannesTschofenig 0:796d0f61a05b 371 int ret;
HannesTschofenig 0:796d0f61a05b 372 pk_context ctx;
HannesTschofenig 0:796d0f61a05b 373 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
HannesTschofenig 0:796d0f61a05b 374 if( ( ret = rsa_copy( pk_rsa( ctx ), rsa ) ) != 0 ) return( ret );
HannesTschofenig 0:796d0f61a05b 375 ret = pk_write_pubkey_der( &ctx, buf, len );
HannesTschofenig 0:796d0f61a05b 376 pk_free( &ctx );
HannesTschofenig 0:796d0f61a05b 377 return( ret );
HannesTschofenig 0:796d0f61a05b 378 }
HannesTschofenig 0:796d0f61a05b 379 static inline int x509_write_key_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
HannesTschofenig 0:796d0f61a05b 380 int ret;
HannesTschofenig 0:796d0f61a05b 381 pk_context ctx;
HannesTschofenig 0:796d0f61a05b 382 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
HannesTschofenig 0:796d0f61a05b 383 if( ( ret = rsa_copy( pk_rsa( ctx ), rsa ) ) != 0 ) return( ret );
HannesTschofenig 0:796d0f61a05b 384 ret = pk_write_key_der( &ctx, buf, len );
HannesTschofenig 0:796d0f61a05b 385 pk_free( &ctx );
HannesTschofenig 0:796d0f61a05b 386 return( ret );
HannesTschofenig 0:796d0f61a05b 387 }
HannesTschofenig 0:796d0f61a05b 388 #endif /* POLARSSL_PK_WRITE_C && POLARSSL_RSA_C */
HannesTschofenig 0:796d0f61a05b 389 #endif /* compat-1.2.h */
HannesTschofenig 0:796d0f61a05b 390
HannesTschofenig 0:796d0f61a05b 391