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