blob: 3e9c396dc165666bc8215bce7590aaa22fdce45c [file] [log] [blame]
Paul Bakkerd1a983f2013-09-16 22:26:53 +02001/**
2 * \file compat-1.2.h
3 *
4 * \brief Backwards compatibility header for PolarSSL-1.2 from PolarSSL-1.3
5 *
6 * Copyright (C) 2006-2013, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_COMPAT_1_2_H
28#define POLARSSL_COMPAT_1_2_H
29
30#include "config.h"
31
Paul Bakker51876562013-09-17 14:36:05 +020032// Comment out to disable prototype change warnings
Paul Bakkerd1a983f2013-09-16 22:26:53 +020033#define SHOW_PROTOTYPE_CHANGE_WARNINGS
34
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035#if defined(_MSC_VER) && !defined(inline)
36#define inline _inline
37#else
38#if defined(__ARMCC_VERSION) && !defined(inline)
39#define inline __inline
40#endif /* __ARMCC_VERSION */
41
42#if defined(_MSC_VER)
Paul Bakker4aa40d42013-10-11 10:49:24 +020043// MSVC does not support #warning
44#undef SHOW_PROTOTYPE_CHANGE_WARNINGS
45#endif
46
Paul Bakker51876562013-09-17 14:36:05 +020047#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
48#warning "You can disable these warnings by commenting SHOW_PROTOTYPE_CHANGE_WARNINGS in compat-1.2.h"
49#endif
50
Paul Bakkerd1a983f2013-09-16 22:26:53 +020051#if defined(POLARSSL_SHA256_C)
52#define POLARSSL_SHA2_C
53#include "sha256.h"
54
55/*
56 * SHA-2 -> SHA-256
57 */
58typedef sha256_context sha2_context;
59
60inline void sha2_starts( sha256_context *ctx, int is224 ) {
61 sha256_starts( ctx, is224 );
62}
63inline void sha2_update( sha256_context *ctx, const unsigned char *input,
64 size_t ilen ) {
65 sha256_update( ctx, input, ilen );
66}
67inline void sha2_finish( sha256_context *ctx, unsigned char output[32] ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +020068 sha256_finish( ctx, output );
Paul Bakkerd1a983f2013-09-16 22:26:53 +020069}
70inline int sha2_file( const char *path, unsigned char output[32], int is224 ) {
71 return sha256_file( path, output, is224 );
72}
73inline void sha2( const unsigned char *input, size_t ilen,
74 unsigned char output[32], int is224 ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +020075 sha256( input, ilen, output, is224 );
Paul Bakkerd1a983f2013-09-16 22:26:53 +020076}
77inline void sha2_hmac_starts( sha256_context *ctx, const unsigned char *key,
78 size_t keylen, int is224 ) {
79 sha256_hmac_starts( ctx, key, keylen, is224 );
80}
81inline void sha2_hmac_update( sha256_context *ctx, const unsigned char *input, size_t ilen ) {
82 sha256_hmac_update( ctx, input, ilen );
83}
84inline void sha2_hmac_finish( sha256_context *ctx, unsigned char output[32] ) {
85 sha256_hmac_finish( ctx, output );
86}
87inline void sha2_hmac_reset( sha256_context *ctx ) {
88 sha256_hmac_reset( ctx );
89}
90inline void sha2_hmac( const unsigned char *key, size_t keylen,
91 const unsigned char *input, size_t ilen,
92 unsigned char output[32], int is224 ) {
93 sha256_hmac( key, keylen, input, ilen, output, is224 );
94}
95inline int sha2_self_test( int verbose ) {
96 return sha256_self_test( verbose );
97}
98#endif /* POLARSSL_SHA256_C */
99
100#if defined(POLARSSL_SHA512_C)
101#define POLARSSL_SHA4_C
102#include "sha512.h"
103
104/*
105 * SHA-4 -> SHA-512
106 */
107typedef sha512_context sha4_context;
108
109inline void sha4_starts( sha512_context *ctx, int is384 ) {
110 sha512_starts( ctx, is384 );
111}
112inline void sha4_update( sha512_context *ctx, const unsigned char *input,
113 size_t ilen ) {
114 sha512_update( ctx, input, ilen );
115}
116inline void sha4_finish( sha512_context *ctx, unsigned char output[64] ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200117 sha512_finish( ctx, output );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200118}
119inline int sha4_file( const char *path, unsigned char output[64], int is384 ) {
120 return sha512_file( path, output, is384 );
121}
122inline void sha4( const unsigned char *input, size_t ilen,
123 unsigned char output[32], int is384 ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200124 sha512( input, ilen, output, is384 );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200125}
126inline void sha4_hmac_starts( sha512_context *ctx, const unsigned char *key,
127 size_t keylen, int is384 ) {
128 sha512_hmac_starts( ctx, key, keylen, is384 );
129}
130inline void sha4_hmac_update( sha512_context *ctx, const unsigned char *input, size_t ilen ) {
131 sha512_hmac_update( ctx, input, ilen );
132}
133inline void sha4_hmac_finish( sha512_context *ctx, unsigned char output[64] ) {
134 sha512_hmac_finish( ctx, output );
135}
136inline void sha4_hmac_reset( sha512_context *ctx ) {
137 sha512_hmac_reset( ctx );
138}
139inline void sha4_hmac( const unsigned char *key, size_t keylen,
140 const unsigned char *input, size_t ilen,
141 unsigned char output[64], int is384 ) {
142 sha512_hmac( key, keylen, input, ilen, output, is384 );
143}
144inline int sha4_self_test( int verbose ) {
145 return sha512_self_test( verbose );
146}
147#endif /* POLARSSL_SHA512_C */
148
149#if defined(POLARSSL_CIPHER_C)
150#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
151#warning "cipher_reset() prototype changed. Manual change required if used"
152#endif
153#endif
154
155#if defined(POLARSSL_RSA_C)
156#define SIG_RSA_RAW POLARSSL_MD_NONE
157#define SIG_RSA_MD2 POLARSSL_MD_MD2
158#define SIG_RSA_MD4 POLARSSL_MD_MD4
159#define SIG_RSA_MD5 POLARSSL_MD_MD5
160#define SIG_RSA_SHA1 POLARSSL_MD_SHA1
161#define SIG_RSA_SHA224 POLARSSL_MD_SHA224
162#define SIG_RSA_SHA256 POLARSSL_MD_SHA256
163#define SIG_RSA_SHA384 POLARSSL_MD_SHA384
164#define SIG_RSA_SHA512 POLARSSL_MD_SHA512
165#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
166#warning "rsa_pkcs1_verify() prototype changed. Manual change required if used"
167#warning "rsa_pkcs1_decrypt() prototype changed. Manual change required if used"
168#endif
169#endif
170
171#if defined(POLARSSL_DHM_C)
172#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
173#warning "dhm_calc_secret() prototype changed. Manual change required if used"
174#endif
175#endif
176
177#if defined(POLARSSL_GCM_C)
178#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
179#warning "gcm_init() prototype changed. Manual change required if used"
180#endif
181#endif
182
183#if defined(POLARSSL_SSL_CLI_C)
184#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
185#warning "ssl_set_own_cert() prototype changed. Change to ssl_set_own_cert_rsa(). Manual change required if used"
186#endif
187#endif
188
Paul Bakker51876562013-09-17 14:36:05 +0200189#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakker86d0c192013-09-18 11:11:02 +0200190#include "x509.h"
191
Paul Bakker51876562013-09-17 14:36:05 +0200192#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT POLARSSL_ERR_X509_INVALID_FORMAT
193#define POLARSSL_ERR_X509_CERT_INVALID_VERSION POLARSSL_ERR_X509_INVALID_VERSION
194#define POLARSSL_ERR_X509_CERT_INVALID_ALG POLARSSL_ERR_X509_INVALID_ALG
195#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG POLARSSL_ERR_X509_UNKNOWN_SIG_ALG
196#define POLARSSL_ERR_X509_CERT_INVALID_NAME POLARSSL_ERR_X509_INVALID_NAME
197#define POLARSSL_ERR_X509_CERT_INVALID_DATE POLARSSL_ERR_X509_INVALID_DATE
198#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS POLARSSL_ERR_X509_INVALID_EXTENSIONS
199#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH POLARSSL_ERR_X509_SIG_MISMATCH
200#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE POLARSSL_ERR_X509_INVALID_SIGNATURE
201#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL POLARSSL_ERR_X509_INVALID_SERIAL
202#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION POLARSSL_ERR_X509_UNKNOWN_VERSION
Paul Bakker86d0c192013-09-18 11:11:02 +0200203
Paul Bakkerddf26b42013-09-18 13:46:23 +0200204inline int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200205 return x509_serial_gets( buf, size, serial );
206}
Paul Bakkerddf26b42013-09-18 13:46:23 +0200207inline int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200208 return x509_dn_gets( buf, size, dn );
209}
Paul Bakkerddf26b42013-09-18 13:46:23 +0200210inline int x509parse_time_expired( const x509_time *time ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200211 return x509_time_expired( time );
212}
Paul Bakker51876562013-09-17 14:36:05 +0200213#endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
214
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200215#if defined(POLARSSL_X509_CRT_PARSE_C)
216#define POLARSSL_X509_PARSE_C
217#include "x509_crt.h"
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200218typedef x509_crt x509_cert;
219
Paul Bakkerddf26b42013-09-18 13:46:23 +0200220inline int x509parse_crt_der( x509_cert *chain, const unsigned char *buf,
221 size_t buflen ) {
222 return x509_crt_parse_der( chain, buf, buflen );
223}
224inline int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen ) {
225 return x509_crt_parse( chain, buf, buflen );
226}
227inline int x509parse_crtfile( x509_cert *chain, const char *path ) {
228 return x509_crt_parse_file( chain, path );
229}
230inline int x509parse_crtpath( x509_cert *chain, const char *path ) {
231 return x509_crt_parse_path( chain, path );
232}
233inline int x509parse_cert_info( char *buf, size_t size, const char *prefix,
234 const x509_cert *crt ) {
235 return x509_crt_info( buf, size, prefix, crt );
236}
237inline int x509parse_verify( x509_cert *crt, x509_cert *trust_ca,
238 x509_crl *ca_crl, const char *cn, int *flags,
239 int (*f_vrfy)(void *, x509_cert *, int, int *),
240 void *p_vrfy ) {
241 return x509_crt_verify( crt, trust_ca, ca_crl, cn, flags, f_vrfy, p_vrfy );
242}
243inline int x509parse_revoked( const x509_cert *crt, const x509_crl *crl ) {
244 return x509_crt_revoked( crt, crl );
245}
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200246inline void x509_free( x509_cert *crt ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200247 x509_crt_free( crt );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200248}
249#endif /* POLARSSL_X509_CRT_PARSE_C */
250
Paul Bakkerddf26b42013-09-18 13:46:23 +0200251#if defined(POLARSSL_X509_CRL_PARSE_C)
252#define POLARSSL_X509_PARSE_C
253#include "x509_crl.h"
254inline int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen ) {
255 return x509_crl_parse( chain, buf, buflen );
256}
257inline int x509parse_crlfile( x509_crl *chain, const char *path ) {
258 return x509_crl_parse_file( chain, path );
259}
260inline int x509parse_crl_info( char *buf, size_t size, const char *prefix,
261 const x509_crl *crl ) {
262 return x509_crl_info( buf, size, prefix, crl );
263}
264#endif /* POLARSSL_X509_CRL_PARSE_C */
265
266#if defined(POLARSSL_X509_CSR_PARSE_C)
267#define POLARSSL_X509_PARSE_C
268#include "x509_csr.h"
269inline int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen ) {
270 return x509_csr_parse( csr, buf, buflen );
271}
272inline int x509parse_csrfile( x509_csr *csr, const char *path ) {
273 return x509_csr_parse_file( csr, path );
274}
275inline int x509parse_csr_info( char *buf, size_t size, const char *prefix,
276 const x509_csr *csr ) {
277 return x509_csr_info( buf, size, prefix, csr );
278}
279#endif /* POLARSSL_X509_CSR_PARSE_C */
280
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200281#if defined(POLARSSL_SSL_TLS_C)
282#include "ssl_ciphersuites.h"
283
284#define ssl_default_ciphersuites ssl_list_ciphersuites()
285#endif
286
287#if defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_RSA_C)
288#include "rsa.h"
289#include "pk.h"
290
291#define POLARSSL_ERR_X509_PASSWORD_MISMATCH POLARSSL_ERR_PK_PASSWORD_MISMATCH
292#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT POLARSSL_ERR_PK_KEY_INVALID_FORMAT
293#define POLARSSL_ERR_X509_UNKNOWN_PK_ALG POLARSSL_ERR_PK_UNKNOWN_PK_ALG
294#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY POLARSSL_ERR_PK_INVALID_PUBKEY
295
296#if defined(POLARSSL_FS_IO)
297inline int x509parse_keyfile( rsa_context *rsa, const char *path,
298 const char *pwd ) {
299 int ret;
300 pk_context pk;
301 pk_init( &pk );
302 ret = pk_parse_keyfile( &pk, path, pwd );
303 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
304 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
305 if( ret == 0 )
306 rsa_copy( rsa, pk_rsa( pk ) );
307 else
308 rsa_free( rsa );
309 pk_free( &pk );
310 return( ret );
311}
312inline int x509parse_public_keyfile( rsa_context *rsa, const char *path ) {
313 int ret;
314 pk_context pk;
315 pk_init( &pk );
316 ret = pk_parse_public_keyfile( &pk, path );
317 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
318 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
319 if( ret == 0 )
320 rsa_copy( rsa, pk_rsa( pk ) );
321 else
322 rsa_free( rsa );
323 pk_free( &pk );
324 return( ret );
325}
326#endif /* POLARSSL_FS_IO */
327
328inline int x509parse_key( rsa_context *rsa, const unsigned char *key,
329 size_t keylen,
330 const unsigned char *pwd, size_t pwdlen ) {
331 int ret;
332 pk_context pk;
333 pk_init( &pk );
334 ret = pk_parse_key( &pk, key, keylen, pwd, pwdlen );
335 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
336 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
337 if( ret == 0 )
338 rsa_copy( rsa, pk_rsa( pk ) );
339 else
340 rsa_free( rsa );
341 pk_free( &pk );
342 return( ret );
343}
344
345inline int x509parse_public_key( rsa_context *rsa,
346 const unsigned char *key, size_t keylen )
347{
348 int ret;
349 pk_context pk;
350 pk_init( &pk );
351 ret = pk_parse_public_key( &pk, key, keylen );
352 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
353 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
354 if( ret == 0 )
355 rsa_copy( rsa, pk_rsa( pk ) );
356 else
357 rsa_free( rsa );
358 pk_free( &pk );
359 return( ret );
360}
361#endif /* POLARSSL_PK_PARSE_C && POLARSSL_RSA_C */
362
363#if defined(POLARSSL_PK_WRITE_C) && defined(POLARSSL_RSA_C)
364#include "pk.h"
365inline int x509_write_pubkey_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
366 int ret;
367 pk_context ctx;
368 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200369 if( ( ret = rsa_copy( pk_rsa( ctx ), rsa ) ) != 0 ) return( ret );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200370 ret = pk_write_pubkey_der( &ctx, buf, len );
371 pk_free( &ctx );
372 return( ret );
373}
374inline int x509_write_key_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
375 int ret;
376 pk_context ctx;
377 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200378 if( ( ret = rsa_copy( pk_rsa( ctx ), rsa ) ) != 0 ) return( ret );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200379 ret = pk_write_key_der( &ctx, buf, len );
380 pk_free( &ctx );
381 return( ret );
382}
383#endif /* POLARSSL_PK_WRITE_C && POLARSSL_RSA_C */
384#endif /* compat-1.2.h */