blob: 372f714474a86b04d0bafd0f40c8477e219085fa [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 Bakkerddf26b42013-09-18 13:46:23 +0200206inline int x509parse_crt_der( x509_cert *chain, const unsigned char *buf,
207 size_t buflen ) {
208 return x509_crt_parse_der( chain, buf, buflen );
209}
210inline int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen ) {
211 return x509_crt_parse( chain, buf, buflen );
212}
213inline int x509parse_crtfile( x509_cert *chain, const char *path ) {
214 return x509_crt_parse_file( chain, path );
215}
216inline int x509parse_crtpath( x509_cert *chain, const char *path ) {
217 return x509_crt_parse_path( chain, path );
218}
219inline int x509parse_cert_info( char *buf, size_t size, const char *prefix,
220 const x509_cert *crt ) {
221 return x509_crt_info( buf, size, prefix, crt );
222}
223inline int x509parse_verify( x509_cert *crt, x509_cert *trust_ca,
224 x509_crl *ca_crl, const char *cn, int *flags,
225 int (*f_vrfy)(void *, x509_cert *, int, int *),
226 void *p_vrfy ) {
227 return x509_crt_verify( crt, trust_ca, ca_crl, cn, flags, f_vrfy, p_vrfy );
228}
229inline int x509parse_revoked( const x509_cert *crt, const x509_crl *crl ) {
230 return x509_crt_revoked( crt, crl );
231}
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200232inline void x509_free( x509_cert *crt ) {
233 return x509_crt_free( crt );
234}
235#endif /* POLARSSL_X509_CRT_PARSE_C */
236
Paul Bakkerddf26b42013-09-18 13:46:23 +0200237#if defined(POLARSSL_X509_CRL_PARSE_C)
238#define POLARSSL_X509_PARSE_C
239#include "x509_crl.h"
240inline int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen ) {
241 return x509_crl_parse( chain, buf, buflen );
242}
243inline int x509parse_crlfile( x509_crl *chain, const char *path ) {
244 return x509_crl_parse_file( chain, path );
245}
246inline int x509parse_crl_info( char *buf, size_t size, const char *prefix,
247 const x509_crl *crl ) {
248 return x509_crl_info( buf, size, prefix, crl );
249}
250#endif /* POLARSSL_X509_CRL_PARSE_C */
251
252#if defined(POLARSSL_X509_CSR_PARSE_C)
253#define POLARSSL_X509_PARSE_C
254#include "x509_csr.h"
255inline int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen ) {
256 return x509_csr_parse( csr, buf, buflen );
257}
258inline int x509parse_csrfile( x509_csr *csr, const char *path ) {
259 return x509_csr_parse_file( csr, path );
260}
261inline int x509parse_csr_info( char *buf, size_t size, const char *prefix,
262 const x509_csr *csr ) {
263 return x509_csr_info( buf, size, prefix, csr );
264}
265#endif /* POLARSSL_X509_CSR_PARSE_C */
266
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200267#if defined(POLARSSL_SSL_TLS_C)
268#include "ssl_ciphersuites.h"
269
270#define ssl_default_ciphersuites ssl_list_ciphersuites()
271#endif
272
273#if defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_RSA_C)
274#include "rsa.h"
275#include "pk.h"
276
277#define POLARSSL_ERR_X509_PASSWORD_MISMATCH POLARSSL_ERR_PK_PASSWORD_MISMATCH
278#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT POLARSSL_ERR_PK_KEY_INVALID_FORMAT
279#define POLARSSL_ERR_X509_UNKNOWN_PK_ALG POLARSSL_ERR_PK_UNKNOWN_PK_ALG
280#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY POLARSSL_ERR_PK_INVALID_PUBKEY
281
282#if defined(POLARSSL_FS_IO)
283inline int x509parse_keyfile( rsa_context *rsa, const char *path,
284 const char *pwd ) {
285 int ret;
286 pk_context pk;
287 pk_init( &pk );
288 ret = pk_parse_keyfile( &pk, path, pwd );
289 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
290 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
291 if( ret == 0 )
292 rsa_copy( rsa, pk_rsa( pk ) );
293 else
294 rsa_free( rsa );
295 pk_free( &pk );
296 return( ret );
297}
298inline int x509parse_public_keyfile( rsa_context *rsa, const char *path ) {
299 int ret;
300 pk_context pk;
301 pk_init( &pk );
302 ret = pk_parse_public_keyfile( &pk, path );
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}
312#endif /* POLARSSL_FS_IO */
313
314inline int x509parse_key( rsa_context *rsa, const unsigned char *key,
315 size_t keylen,
316 const unsigned char *pwd, size_t pwdlen ) {
317 int ret;
318 pk_context pk;
319 pk_init( &pk );
320 ret = pk_parse_key( &pk, key, keylen, pwd, pwdlen );
321 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
322 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
323 if( ret == 0 )
324 rsa_copy( rsa, pk_rsa( pk ) );
325 else
326 rsa_free( rsa );
327 pk_free( &pk );
328 return( ret );
329}
330
331inline int x509parse_public_key( rsa_context *rsa,
332 const unsigned char *key, size_t keylen )
333{
334 int ret;
335 pk_context pk;
336 pk_init( &pk );
337 ret = pk_parse_public_key( &pk, key, keylen );
338 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
339 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
340 if( ret == 0 )
341 rsa_copy( rsa, pk_rsa( pk ) );
342 else
343 rsa_free( rsa );
344 pk_free( &pk );
345 return( ret );
346}
347#endif /* POLARSSL_PK_PARSE_C && POLARSSL_RSA_C */
348
349#if defined(POLARSSL_PK_WRITE_C) && defined(POLARSSL_RSA_C)
350#include "pk.h"
351inline int x509_write_pubkey_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
352 int ret;
353 pk_context ctx;
354 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
355 if( ( ret = rsa_copy( ctx.pk_ctx, rsa ) ) != 0 ) return( ret );
356 ret = pk_write_pubkey_der( &ctx, buf, len );
357 pk_free( &ctx );
358 return( ret );
359}
360inline int x509_write_key_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
361 int ret;
362 pk_context ctx;
363 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
364 if( ( ret = rsa_copy( ctx.pk_ctx, rsa ) ) != 0 ) return( ret );
365 ret = pk_write_key_der( &ctx, buf, len );
366 pk_free( &ctx );
367 return( ret );
368}
369#endif /* POLARSSL_PK_WRITE_C && POLARSSL_RSA_C */
370#endif /* compat-1.2.h */