blob: c0d7e01e5a7a66bb96a2a2760109ad7c4a069b36 [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
32#define SHOW_PROTOTYPE_CHANGE_WARNINGS
33
34#if defined(POLARSSL_SHA256_C)
35#define POLARSSL_SHA2_C
36#include "sha256.h"
37
38/*
39 * SHA-2 -> SHA-256
40 */
41typedef sha256_context sha2_context;
42
43inline void sha2_starts( sha256_context *ctx, int is224 ) {
44 sha256_starts( ctx, is224 );
45}
46inline void sha2_update( sha256_context *ctx, const unsigned char *input,
47 size_t ilen ) {
48 sha256_update( ctx, input, ilen );
49}
50inline void sha2_finish( sha256_context *ctx, unsigned char output[32] ) {
51 return sha256_finish( ctx, output );
52}
53inline int sha2_file( const char *path, unsigned char output[32], int is224 ) {
54 return sha256_file( path, output, is224 );
55}
56inline void sha2( const unsigned char *input, size_t ilen,
57 unsigned char output[32], int is224 ) {
58 return sha256( input, ilen, output, is224 );
59}
60inline void sha2_hmac_starts( sha256_context *ctx, const unsigned char *key,
61 size_t keylen, int is224 ) {
62 sha256_hmac_starts( ctx, key, keylen, is224 );
63}
64inline void sha2_hmac_update( sha256_context *ctx, const unsigned char *input, size_t ilen ) {
65 sha256_hmac_update( ctx, input, ilen );
66}
67inline void sha2_hmac_finish( sha256_context *ctx, unsigned char output[32] ) {
68 sha256_hmac_finish( ctx, output );
69}
70inline void sha2_hmac_reset( sha256_context *ctx ) {
71 sha256_hmac_reset( ctx );
72}
73inline void sha2_hmac( const unsigned char *key, size_t keylen,
74 const unsigned char *input, size_t ilen,
75 unsigned char output[32], int is224 ) {
76 sha256_hmac( key, keylen, input, ilen, output, is224 );
77}
78inline int sha2_self_test( int verbose ) {
79 return sha256_self_test( verbose );
80}
81#endif /* POLARSSL_SHA256_C */
82
83#if defined(POLARSSL_SHA512_C)
84#define POLARSSL_SHA4_C
85#include "sha512.h"
86
87/*
88 * SHA-4 -> SHA-512
89 */
90typedef sha512_context sha4_context;
91
92inline void sha4_starts( sha512_context *ctx, int is384 ) {
93 sha512_starts( ctx, is384 );
94}
95inline void sha4_update( sha512_context *ctx, const unsigned char *input,
96 size_t ilen ) {
97 sha512_update( ctx, input, ilen );
98}
99inline void sha4_finish( sha512_context *ctx, unsigned char output[64] ) {
100 return sha512_finish( ctx, output );
101}
102inline int sha4_file( const char *path, unsigned char output[64], int is384 ) {
103 return sha512_file( path, output, is384 );
104}
105inline void sha4( const unsigned char *input, size_t ilen,
106 unsigned char output[32], int is384 ) {
107 return sha512( input, ilen, output, is384 );
108}
109inline void sha4_hmac_starts( sha512_context *ctx, const unsigned char *key,
110 size_t keylen, int is384 ) {
111 sha512_hmac_starts( ctx, key, keylen, is384 );
112}
113inline void sha4_hmac_update( sha512_context *ctx, const unsigned char *input, size_t ilen ) {
114 sha512_hmac_update( ctx, input, ilen );
115}
116inline void sha4_hmac_finish( sha512_context *ctx, unsigned char output[64] ) {
117 sha512_hmac_finish( ctx, output );
118}
119inline void sha4_hmac_reset( sha512_context *ctx ) {
120 sha512_hmac_reset( ctx );
121}
122inline void sha4_hmac( const unsigned char *key, size_t keylen,
123 const unsigned char *input, size_t ilen,
124 unsigned char output[64], int is384 ) {
125 sha512_hmac( key, keylen, input, ilen, output, is384 );
126}
127inline int sha4_self_test( int verbose ) {
128 return sha512_self_test( verbose );
129}
130#endif /* POLARSSL_SHA512_C */
131
132#if defined(POLARSSL_CIPHER_C)
133#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
134#warning "cipher_reset() prototype changed. Manual change required if used"
135#endif
136#endif
137
138#if defined(POLARSSL_RSA_C)
139#define SIG_RSA_RAW POLARSSL_MD_NONE
140#define SIG_RSA_MD2 POLARSSL_MD_MD2
141#define SIG_RSA_MD4 POLARSSL_MD_MD4
142#define SIG_RSA_MD5 POLARSSL_MD_MD5
143#define SIG_RSA_SHA1 POLARSSL_MD_SHA1
144#define SIG_RSA_SHA224 POLARSSL_MD_SHA224
145#define SIG_RSA_SHA256 POLARSSL_MD_SHA256
146#define SIG_RSA_SHA384 POLARSSL_MD_SHA384
147#define SIG_RSA_SHA512 POLARSSL_MD_SHA512
148#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
149#warning "rsa_pkcs1_verify() prototype changed. Manual change required if used"
150#warning "rsa_pkcs1_decrypt() prototype changed. Manual change required if used"
151#endif
152#endif
153
154#if defined(POLARSSL_DHM_C)
155#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
156#warning "dhm_calc_secret() prototype changed. Manual change required if used"
157#endif
158#endif
159
160#if defined(POLARSSL_GCM_C)
161#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
162#warning "gcm_init() prototype changed. Manual change required if used"
163#endif
164#endif
165
166#if defined(POLARSSL_SSL_CLI_C)
167#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
168#warning "ssl_set_own_cert() prototype changed. Change to ssl_set_own_cert_rsa(). Manual change required if used"
169#endif
170#endif
171
172#if defined(POLARSSL_X509_CRT_PARSE_C)
173#define POLARSSL_X509_PARSE_C
174#include "x509_crt.h"
175
176inline void x509_free( x509_cert *crt ) {
177 return x509_crt_free( crt );
178}
179#endif /* POLARSSL_X509_CRT_PARSE_C */
180
181#if defined(POLARSSL_SSL_TLS_C)
182#include "ssl_ciphersuites.h"
183
184#define ssl_default_ciphersuites ssl_list_ciphersuites()
185#endif
186
187#if defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_RSA_C)
188#include "rsa.h"
189#include "pk.h"
190
191#define POLARSSL_ERR_X509_PASSWORD_MISMATCH POLARSSL_ERR_PK_PASSWORD_MISMATCH
192#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT POLARSSL_ERR_PK_KEY_INVALID_FORMAT
193#define POLARSSL_ERR_X509_UNKNOWN_PK_ALG POLARSSL_ERR_PK_UNKNOWN_PK_ALG
194#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY POLARSSL_ERR_PK_INVALID_PUBKEY
195
196#if defined(POLARSSL_FS_IO)
197inline int x509parse_keyfile( rsa_context *rsa, const char *path,
198 const char *pwd ) {
199 int ret;
200 pk_context pk;
201 pk_init( &pk );
202 ret = pk_parse_keyfile( &pk, path, pwd );
203 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
204 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
205 if( ret == 0 )
206 rsa_copy( rsa, pk_rsa( pk ) );
207 else
208 rsa_free( rsa );
209 pk_free( &pk );
210 return( ret );
211}
212inline int x509parse_public_keyfile( rsa_context *rsa, const char *path ) {
213 int ret;
214 pk_context pk;
215 pk_init( &pk );
216 ret = pk_parse_public_keyfile( &pk, path );
217 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
218 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
219 if( ret == 0 )
220 rsa_copy( rsa, pk_rsa( pk ) );
221 else
222 rsa_free( rsa );
223 pk_free( &pk );
224 return( ret );
225}
226#endif /* POLARSSL_FS_IO */
227
228inline int x509parse_key( rsa_context *rsa, const unsigned char *key,
229 size_t keylen,
230 const unsigned char *pwd, size_t pwdlen ) {
231 int ret;
232 pk_context pk;
233 pk_init( &pk );
234 ret = pk_parse_key( &pk, key, keylen, pwd, pwdlen );
235 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
236 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
237 if( ret == 0 )
238 rsa_copy( rsa, pk_rsa( pk ) );
239 else
240 rsa_free( rsa );
241 pk_free( &pk );
242 return( ret );
243}
244
245inline int x509parse_public_key( rsa_context *rsa,
246 const unsigned char *key, size_t keylen )
247{
248 int ret;
249 pk_context pk;
250 pk_init( &pk );
251 ret = pk_parse_public_key( &pk, key, keylen );
252 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
253 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
254 if( ret == 0 )
255 rsa_copy( rsa, pk_rsa( pk ) );
256 else
257 rsa_free( rsa );
258 pk_free( &pk );
259 return( ret );
260}
261#endif /* POLARSSL_PK_PARSE_C && POLARSSL_RSA_C */
262
263#if defined(POLARSSL_PK_WRITE_C) && defined(POLARSSL_RSA_C)
264#include "pk.h"
265inline int x509_write_pubkey_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
266 int ret;
267 pk_context ctx;
268 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
269 if( ( ret = rsa_copy( ctx.pk_ctx, rsa ) ) != 0 ) return( ret );
270 ret = pk_write_pubkey_der( &ctx, buf, len );
271 pk_free( &ctx );
272 return( ret );
273}
274inline int x509_write_key_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
275 int ret;
276 pk_context ctx;
277 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
278 if( ( ret = rsa_copy( ctx.pk_ctx, rsa ) ) != 0 ) return( ret );
279 ret = pk_write_key_der( &ctx, buf, len );
280 pk_free( &ctx );
281 return( ret );
282}
283#endif /* POLARSSL_PK_WRITE_C && POLARSSL_RSA_C */
284#endif /* compat-1.2.h */