blob: 8b2a0165adbca90739c0c9b37747d990d7187ca2 [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
192int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial ) {
193 return x509_serial_gets( buf, size, serial );
194}
195int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn ) {
196 return x509_dn_gets( buf, size, dn );
197}
198int x509parse_time_expired( const x509_time *time ) {
199 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"
206
207inline void x509_free( x509_cert *crt ) {
208 return x509_crt_free( crt );
209}
210#endif /* POLARSSL_X509_CRT_PARSE_C */
211
212#if defined(POLARSSL_SSL_TLS_C)
213#include "ssl_ciphersuites.h"
214
215#define ssl_default_ciphersuites ssl_list_ciphersuites()
216#endif
217
218#if defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_RSA_C)
219#include "rsa.h"
220#include "pk.h"
221
222#define POLARSSL_ERR_X509_PASSWORD_MISMATCH POLARSSL_ERR_PK_PASSWORD_MISMATCH
223#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT POLARSSL_ERR_PK_KEY_INVALID_FORMAT
224#define POLARSSL_ERR_X509_UNKNOWN_PK_ALG POLARSSL_ERR_PK_UNKNOWN_PK_ALG
225#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY POLARSSL_ERR_PK_INVALID_PUBKEY
226
227#if defined(POLARSSL_FS_IO)
228inline int x509parse_keyfile( rsa_context *rsa, const char *path,
229 const char *pwd ) {
230 int ret;
231 pk_context pk;
232 pk_init( &pk );
233 ret = pk_parse_keyfile( &pk, path, pwd );
234 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
235 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
236 if( ret == 0 )
237 rsa_copy( rsa, pk_rsa( pk ) );
238 else
239 rsa_free( rsa );
240 pk_free( &pk );
241 return( ret );
242}
243inline int x509parse_public_keyfile( rsa_context *rsa, const char *path ) {
244 int ret;
245 pk_context pk;
246 pk_init( &pk );
247 ret = pk_parse_public_keyfile( &pk, path );
248 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
249 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
250 if( ret == 0 )
251 rsa_copy( rsa, pk_rsa( pk ) );
252 else
253 rsa_free( rsa );
254 pk_free( &pk );
255 return( ret );
256}
257#endif /* POLARSSL_FS_IO */
258
259inline int x509parse_key( rsa_context *rsa, const unsigned char *key,
260 size_t keylen,
261 const unsigned char *pwd, size_t pwdlen ) {
262 int ret;
263 pk_context pk;
264 pk_init( &pk );
265 ret = pk_parse_key( &pk, key, keylen, pwd, pwdlen );
266 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
267 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
268 if( ret == 0 )
269 rsa_copy( rsa, pk_rsa( pk ) );
270 else
271 rsa_free( rsa );
272 pk_free( &pk );
273 return( ret );
274}
275
276inline int x509parse_public_key( rsa_context *rsa,
277 const unsigned char *key, size_t keylen )
278{
279 int ret;
280 pk_context pk;
281 pk_init( &pk );
282 ret = pk_parse_public_key( &pk, key, keylen );
283 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
284 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
285 if( ret == 0 )
286 rsa_copy( rsa, pk_rsa( pk ) );
287 else
288 rsa_free( rsa );
289 pk_free( &pk );
290 return( ret );
291}
292#endif /* POLARSSL_PK_PARSE_C && POLARSSL_RSA_C */
293
294#if defined(POLARSSL_PK_WRITE_C) && defined(POLARSSL_RSA_C)
295#include "pk.h"
296inline int x509_write_pubkey_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
297 int ret;
298 pk_context ctx;
299 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
300 if( ( ret = rsa_copy( ctx.pk_ctx, rsa ) ) != 0 ) return( ret );
301 ret = pk_write_pubkey_der( &ctx, buf, len );
302 pk_free( &ctx );
303 return( ret );
304}
305inline int x509_write_key_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
306 int ret;
307 pk_context ctx;
308 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
309 if( ( ret = rsa_copy( ctx.pk_ctx, rsa ) ) != 0 ) return( ret );
310 ret = pk_write_key_der( &ctx, buf, len );
311 pk_free( &ctx );
312 return( ret );
313}
314#endif /* POLARSSL_PK_WRITE_C && POLARSSL_RSA_C */
315#endif /* compat-1.2.h */