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