blob: 37df5412f6bcef3b85e09f858fe8dc5a9752b865 [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 *
Manuel Pégourié-Gonnarde6581762015-03-20 17:21:21 +00006 * \deprecated Use native PolarSSL 1.3 functions instead.
7 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00008 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerd1a983f2013-09-16 22:26:53 +02009 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000010 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerd1a983f2013-09-16 22:26:53 +020011 *
Paul Bakkerd1a983f2013-09-16 22:26:53 +020012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
Manuel Pégourié-Gonnarda1e32412015-04-15 11:03:43 +020026#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard8c3f0f42015-04-09 14:10:26 +020027
Manuel Pégourié-Gonnarda1e32412015-04-15 11:03:43 +020028#if defined(POLARSSL_DEPRECATED_WARNING)
Manuel Pégourié-Gonnard8c3f0f42015-04-09 14:10:26 +020029#warning "Including compat-1.2.h is deprecated"
30#endif
31
Paul Bakkerd1a983f2013-09-16 22:26:53 +020032#ifndef POLARSSL_COMPAT_1_2_H
33#define POLARSSL_COMPAT_1_2_H
34
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerd1a983f2013-09-16 22:26:53 +020036#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#else
38#include POLARSSL_CONFIG_FILE
39#endif
Paul Bakkerd1a983f2013-09-16 22:26:53 +020040
Paul Bakker51876562013-09-17 14:36:05 +020041// Comment out to disable prototype change warnings
Paul Bakkerd1a983f2013-09-16 22:26:53 +020042#define SHOW_PROTOTYPE_CHANGE_WARNINGS
43
Manuel Pégourié-Gonnard20607bb2015-10-05 11:40:01 +010044#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
45 !defined(inline) && !defined(__cplusplus)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010046#define inline __inline
Manuel Pégourié-Gonnard20607bb2015-10-05 11:40:01 +010047#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +010048
49#if defined(_MSC_VER)
Paul Bakker4aa40d42013-10-11 10:49:24 +020050// MSVC does not support #warning
51#undef SHOW_PROTOTYPE_CHANGE_WARNINGS
52#endif
53
Paul Bakker51876562013-09-17 14:36:05 +020054#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
55#warning "You can disable these warnings by commenting SHOW_PROTOTYPE_CHANGE_WARNINGS in compat-1.2.h"
56#endif
57
Paul Bakkerd1a983f2013-09-16 22:26:53 +020058#if defined(POLARSSL_SHA256_C)
59#define POLARSSL_SHA2_C
60#include "sha256.h"
61
62/*
63 * SHA-2 -> SHA-256
64 */
65typedef sha256_context sha2_context;
66
Steffan Karger44cf68f2013-11-12 10:34:55 +010067static inline void sha2_starts( sha256_context *ctx, int is224 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020068 sha256_starts( ctx, is224 );
69}
Steffan Karger44cf68f2013-11-12 10:34:55 +010070static inline void sha2_update( sha256_context *ctx, const unsigned char *input,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020071 size_t ilen ) {
72 sha256_update( ctx, input, ilen );
73}
Steffan Karger44cf68f2013-11-12 10:34:55 +010074static inline void sha2_finish( sha256_context *ctx, unsigned char output[32] ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +020075 sha256_finish( ctx, output );
Paul Bakkerd1a983f2013-09-16 22:26:53 +020076}
Steffan Karger44cf68f2013-11-12 10:34:55 +010077static inline int sha2_file( const char *path, unsigned char output[32], int is224 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020078 return sha256_file( path, output, is224 );
79}
Steffan Karger44cf68f2013-11-12 10:34:55 +010080static inline void sha2( const unsigned char *input, size_t ilen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020081 unsigned char output[32], int is224 ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +020082 sha256( input, ilen, output, is224 );
Paul Bakkerd1a983f2013-09-16 22:26:53 +020083}
Steffan Karger44cf68f2013-11-12 10:34:55 +010084static inline void sha2_hmac_starts( sha256_context *ctx, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020085 size_t keylen, int is224 ) {
86 sha256_hmac_starts( ctx, key, keylen, is224 );
87}
Steffan Karger44cf68f2013-11-12 10:34:55 +010088static inline void sha2_hmac_update( sha256_context *ctx, const unsigned char *input, size_t ilen ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020089 sha256_hmac_update( ctx, input, ilen );
90}
Steffan Karger44cf68f2013-11-12 10:34:55 +010091static inline void sha2_hmac_finish( sha256_context *ctx, unsigned char output[32] ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020092 sha256_hmac_finish( ctx, output );
93}
Steffan Karger44cf68f2013-11-12 10:34:55 +010094static inline void sha2_hmac_reset( sha256_context *ctx ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020095 sha256_hmac_reset( ctx );
96}
Steffan Karger44cf68f2013-11-12 10:34:55 +010097static inline void sha2_hmac( const unsigned char *key, size_t keylen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020098 const unsigned char *input, size_t ilen,
99 unsigned char output[32], int is224 ) {
100 sha256_hmac( key, keylen, input, ilen, output, is224 );
101}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100102static inline int sha2_self_test( int verbose ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200103 return sha256_self_test( verbose );
104}
105#endif /* POLARSSL_SHA256_C */
106
107#if defined(POLARSSL_SHA512_C)
108#define POLARSSL_SHA4_C
109#include "sha512.h"
110
111/*
112 * SHA-4 -> SHA-512
113 */
114typedef sha512_context sha4_context;
115
Steffan Karger44cf68f2013-11-12 10:34:55 +0100116static inline void sha4_starts( sha512_context *ctx, int is384 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200117 sha512_starts( ctx, is384 );
118}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100119static inline void sha4_update( sha512_context *ctx, const unsigned char *input,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200120 size_t ilen ) {
121 sha512_update( ctx, input, ilen );
122}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100123static inline void sha4_finish( sha512_context *ctx, unsigned char output[64] ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200124 sha512_finish( ctx, output );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200125}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100126static inline int sha4_file( const char *path, unsigned char output[64], int is384 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200127 return sha512_file( path, output, is384 );
128}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100129static inline void sha4( const unsigned char *input, size_t ilen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200130 unsigned char output[32], int is384 ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200131 sha512( input, ilen, output, is384 );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200132}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100133static inline void sha4_hmac_starts( sha512_context *ctx, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200134 size_t keylen, int is384 ) {
135 sha512_hmac_starts( ctx, key, keylen, is384 );
136}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100137static inline void sha4_hmac_update( sha512_context *ctx, const unsigned char *input, size_t ilen ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200138 sha512_hmac_update( ctx, input, ilen );
139}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100140static inline void sha4_hmac_finish( sha512_context *ctx, unsigned char output[64] ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200141 sha512_hmac_finish( ctx, output );
142}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100143static inline void sha4_hmac_reset( sha512_context *ctx ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200144 sha512_hmac_reset( ctx );
145}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100146static inline void sha4_hmac( const unsigned char *key, size_t keylen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200147 const unsigned char *input, size_t ilen,
148 unsigned char output[64], int is384 ) {
149 sha512_hmac( key, keylen, input, ilen, output, is384 );
150}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100151static inline int sha4_self_test( int verbose ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200152 return sha512_self_test( verbose );
153}
154#endif /* POLARSSL_SHA512_C */
155
156#if defined(POLARSSL_CIPHER_C)
157#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
158#warning "cipher_reset() prototype changed. Manual change required if used"
159#endif
160#endif
161
162#if defined(POLARSSL_RSA_C)
163#define SIG_RSA_RAW POLARSSL_MD_NONE
164#define SIG_RSA_MD2 POLARSSL_MD_MD2
165#define SIG_RSA_MD4 POLARSSL_MD_MD4
166#define SIG_RSA_MD5 POLARSSL_MD_MD5
167#define SIG_RSA_SHA1 POLARSSL_MD_SHA1
168#define SIG_RSA_SHA224 POLARSSL_MD_SHA224
169#define SIG_RSA_SHA256 POLARSSL_MD_SHA256
170#define SIG_RSA_SHA384 POLARSSL_MD_SHA384
171#define SIG_RSA_SHA512 POLARSSL_MD_SHA512
172#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
173#warning "rsa_pkcs1_verify() prototype changed. Manual change required if used"
174#warning "rsa_pkcs1_decrypt() prototype changed. Manual change required if used"
175#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200176#endif /* POLARSSL_RSA_C */
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200177
178#if defined(POLARSSL_DHM_C)
179#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
180#warning "dhm_calc_secret() prototype changed. Manual change required if used"
181#endif
182#endif
183
184#if defined(POLARSSL_GCM_C)
185#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
186#warning "gcm_init() prototype changed. Manual change required if used"
187#endif
188#endif
189
190#if defined(POLARSSL_SSL_CLI_C)
191#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
192#warning "ssl_set_own_cert() prototype changed. Change to ssl_set_own_cert_rsa(). Manual change required if used"
193#endif
194#endif
195
Paul Bakker51876562013-09-17 14:36:05 +0200196#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakker86d0c192013-09-18 11:11:02 +0200197#include "x509.h"
198
Paul Bakker51876562013-09-17 14:36:05 +0200199#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT POLARSSL_ERR_X509_INVALID_FORMAT
200#define POLARSSL_ERR_X509_CERT_INVALID_VERSION POLARSSL_ERR_X509_INVALID_VERSION
201#define POLARSSL_ERR_X509_CERT_INVALID_ALG POLARSSL_ERR_X509_INVALID_ALG
202#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG POLARSSL_ERR_X509_UNKNOWN_SIG_ALG
203#define POLARSSL_ERR_X509_CERT_INVALID_NAME POLARSSL_ERR_X509_INVALID_NAME
204#define POLARSSL_ERR_X509_CERT_INVALID_DATE POLARSSL_ERR_X509_INVALID_DATE
205#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS POLARSSL_ERR_X509_INVALID_EXTENSIONS
206#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH POLARSSL_ERR_X509_SIG_MISMATCH
207#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE POLARSSL_ERR_X509_INVALID_SIGNATURE
208#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL POLARSSL_ERR_X509_INVALID_SERIAL
209#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION POLARSSL_ERR_X509_UNKNOWN_VERSION
Paul Bakker86d0c192013-09-18 11:11:02 +0200210
Steffan Karger44cf68f2013-11-12 10:34:55 +0100211static inline int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200212 return x509_serial_gets( buf, size, serial );
213}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100214static inline int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200215 return x509_dn_gets( buf, size, dn );
216}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100217static inline int x509parse_time_expired( const x509_time *time ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200218 return x509_time_expired( time );
219}
Paul Bakker51876562013-09-17 14:36:05 +0200220#endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
221
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200222#if defined(POLARSSL_X509_CRT_PARSE_C)
223#define POLARSSL_X509_PARSE_C
224#include "x509_crt.h"
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200225typedef x509_crt x509_cert;
226
Steffan Karger44cf68f2013-11-12 10:34:55 +0100227static inline int x509parse_crt_der( x509_cert *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200228 size_t buflen ) {
229 return x509_crt_parse_der( chain, buf, buflen );
230}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100231static inline int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200232 return x509_crt_parse( chain, buf, buflen );
233}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100234static inline int x509parse_crtfile( x509_cert *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200235 return x509_crt_parse_file( chain, path );
236}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100237static inline int x509parse_crtpath( x509_cert *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200238 return x509_crt_parse_path( chain, path );
239}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100240static inline int x509parse_cert_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200241 const x509_cert *crt ) {
242 return x509_crt_info( buf, size, prefix, crt );
243}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100244static inline int x509parse_verify( x509_cert *crt, x509_cert *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200245 x509_crl *ca_crl, const char *cn, int *flags,
246 int (*f_vrfy)(void *, x509_cert *, int, int *),
247 void *p_vrfy ) {
248 return x509_crt_verify( crt, trust_ca, ca_crl, cn, flags, f_vrfy, p_vrfy );
249}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100250static inline int x509parse_revoked( const x509_cert *crt, const x509_crl *crl ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200251 return x509_crt_revoked( crt, crl );
252}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100253static inline void x509_free( x509_cert *crt ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200254 x509_crt_free( crt );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200255}
256#endif /* POLARSSL_X509_CRT_PARSE_C */
257
Paul Bakkerddf26b42013-09-18 13:46:23 +0200258#if defined(POLARSSL_X509_CRL_PARSE_C)
259#define POLARSSL_X509_PARSE_C
260#include "x509_crl.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100261static inline int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200262 return x509_crl_parse( chain, buf, buflen );
263}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100264static inline int x509parse_crlfile( x509_crl *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200265 return x509_crl_parse_file( chain, path );
266}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100267static inline int x509parse_crl_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200268 const x509_crl *crl ) {
269 return x509_crl_info( buf, size, prefix, crl );
270}
271#endif /* POLARSSL_X509_CRL_PARSE_C */
272
273#if defined(POLARSSL_X509_CSR_PARSE_C)
274#define POLARSSL_X509_PARSE_C
275#include "x509_csr.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100276static inline int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200277 return x509_csr_parse( csr, buf, buflen );
278}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100279static inline int x509parse_csrfile( x509_csr *csr, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200280 return x509_csr_parse_file( csr, path );
281}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100282static inline int x509parse_csr_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200283 const x509_csr *csr ) {
284 return x509_csr_info( buf, size, prefix, csr );
285}
286#endif /* POLARSSL_X509_CSR_PARSE_C */
287
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200288#if defined(POLARSSL_SSL_TLS_C)
289#include "ssl_ciphersuites.h"
290
291#define ssl_default_ciphersuites ssl_list_ciphersuites()
292#endif
293
294#if defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_RSA_C)
295#include "rsa.h"
296#include "pk.h"
297
298#define POLARSSL_ERR_X509_PASSWORD_MISMATCH POLARSSL_ERR_PK_PASSWORD_MISMATCH
299#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT POLARSSL_ERR_PK_KEY_INVALID_FORMAT
300#define POLARSSL_ERR_X509_UNKNOWN_PK_ALG POLARSSL_ERR_PK_UNKNOWN_PK_ALG
301#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY POLARSSL_ERR_PK_INVALID_PUBKEY
302
303#if defined(POLARSSL_FS_IO)
Steffan Karger44cf68f2013-11-12 10:34:55 +0100304static inline int x509parse_keyfile( rsa_context *rsa, const char *path,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200305 const char *pwd ) {
306 int ret;
307 pk_context pk;
308 pk_init( &pk );
309 ret = pk_parse_keyfile( &pk, path, pwd );
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}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100319static inline int x509parse_public_keyfile( rsa_context *rsa, const char *path ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200320 int ret;
321 pk_context pk;
322 pk_init( &pk );
323 ret = pk_parse_public_keyfile( &pk, path );
324 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
325 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
326 if( ret == 0 )
327 rsa_copy( rsa, pk_rsa( pk ) );
328 else
329 rsa_free( rsa );
330 pk_free( &pk );
331 return( ret );
332}
333#endif /* POLARSSL_FS_IO */
334
Steffan Karger44cf68f2013-11-12 10:34:55 +0100335static inline int x509parse_key( rsa_context *rsa, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200336 size_t keylen,
337 const unsigned char *pwd, size_t pwdlen ) {
338 int ret;
339 pk_context pk;
340 pk_init( &pk );
341 ret = pk_parse_key( &pk, key, keylen, pwd, pwdlen );
342 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
343 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
344 if( ret == 0 )
345 rsa_copy( rsa, pk_rsa( pk ) );
346 else
347 rsa_free( rsa );
348 pk_free( &pk );
349 return( ret );
350}
351
Steffan Karger44cf68f2013-11-12 10:34:55 +0100352static inline int x509parse_public_key( rsa_context *rsa,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200353 const unsigned char *key, size_t keylen )
354{
355 int ret;
356 pk_context pk;
357 pk_init( &pk );
358 ret = pk_parse_public_key( &pk, key, keylen );
359 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
360 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
361 if( ret == 0 )
362 rsa_copy( rsa, pk_rsa( pk ) );
363 else
364 rsa_free( rsa );
365 pk_free( &pk );
366 return( ret );
367}
368#endif /* POLARSSL_PK_PARSE_C && POLARSSL_RSA_C */
369
370#if defined(POLARSSL_PK_WRITE_C) && defined(POLARSSL_RSA_C)
371#include "pk.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100372static inline int x509_write_pubkey_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200373 int ret;
374 pk_context ctx;
375 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200376 if( ( ret = rsa_copy( pk_rsa( ctx ), rsa ) ) != 0 ) return( ret );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200377 ret = pk_write_pubkey_der( &ctx, buf, len );
378 pk_free( &ctx );
379 return( ret );
380}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100381static inline int x509_write_key_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200382 int ret;
383 pk_context ctx;
384 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200385 if( ( ret = rsa_copy( pk_rsa( ctx ), rsa ) ) != 0 ) return( ret );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200386 ret = pk_write_key_der( &ctx, buf, len );
387 pk_free( &ctx );
388 return( ret );
389}
390#endif /* POLARSSL_PK_WRITE_C && POLARSSL_RSA_C */
391#endif /* compat-1.2.h */
Manuel Pégourié-Gonnarda1e32412015-04-15 11:03:43 +0200392#endif /* POLARSSL_DEPRECATED_REMOVED */