blob: 441ced7c5742736448b63a8ac2e68e69f4b13cfe [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é-Gonnard8c3f0f42015-04-09 14:10:26 +020026#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
27
28#if defined(MBEDTLS_DEPRECATED_WARNING)
29#warning "Including compat-1.2.h is deprecated"
30#endif
31
32#ifndef MBEDTLS_COMPAT13_H
33#define MBEDTLS_COMPAT13_H
34
Paul Bakkerd1a983f2013-09-16 22:26:53 +020035#ifndef POLARSSL_COMPAT_1_2_H
36#define POLARSSL_COMPAT_1_2_H
37
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerd1a983f2013-09-16 22:26:53 +020039#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020040#else
41#include POLARSSL_CONFIG_FILE
42#endif
Paul Bakkerd1a983f2013-09-16 22:26:53 +020043
Paul Bakker51876562013-09-17 14:36:05 +020044// Comment out to disable prototype change warnings
Paul Bakkerd1a983f2013-09-16 22:26:53 +020045#define SHOW_PROTOTYPE_CHANGE_WARNINGS
46
Paul Bakkerfa6a6202013-10-28 18:48:30 +010047#if defined(_MSC_VER) && !defined(inline)
48#define inline _inline
49#else
50#if defined(__ARMCC_VERSION) && !defined(inline)
51#define inline __inline
52#endif /* __ARMCC_VERSION */
Paul Bakkerd46a9f12013-10-31 14:34:19 +010053#endif /* _MSC_VER */
Paul Bakkerfa6a6202013-10-28 18:48:30 +010054
55#if defined(_MSC_VER)
Paul Bakker4aa40d42013-10-11 10:49:24 +020056// MSVC does not support #warning
57#undef SHOW_PROTOTYPE_CHANGE_WARNINGS
58#endif
59
Paul Bakker51876562013-09-17 14:36:05 +020060#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
61#warning "You can disable these warnings by commenting SHOW_PROTOTYPE_CHANGE_WARNINGS in compat-1.2.h"
62#endif
63
Paul Bakkerd1a983f2013-09-16 22:26:53 +020064#if defined(POLARSSL_SHA256_C)
65#define POLARSSL_SHA2_C
66#include "sha256.h"
67
68/*
69 * SHA-2 -> SHA-256
70 */
71typedef sha256_context sha2_context;
72
Steffan Karger44cf68f2013-11-12 10:34:55 +010073static inline void sha2_starts( sha256_context *ctx, int is224 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020074 sha256_starts( ctx, is224 );
75}
Steffan Karger44cf68f2013-11-12 10:34:55 +010076static inline void sha2_update( sha256_context *ctx, const unsigned char *input,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020077 size_t ilen ) {
78 sha256_update( ctx, input, ilen );
79}
Steffan Karger44cf68f2013-11-12 10:34:55 +010080static inline void sha2_finish( sha256_context *ctx, unsigned char output[32] ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +020081 sha256_finish( ctx, output );
Paul Bakkerd1a983f2013-09-16 22:26:53 +020082}
Steffan Karger44cf68f2013-11-12 10:34:55 +010083static inline int sha2_file( const char *path, unsigned char output[32], int is224 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020084 return sha256_file( path, output, is224 );
85}
Steffan Karger44cf68f2013-11-12 10:34:55 +010086static inline void sha2( const unsigned char *input, size_t ilen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020087 unsigned char output[32], int is224 ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +020088 sha256( input, ilen, output, is224 );
Paul Bakkerd1a983f2013-09-16 22:26:53 +020089}
Steffan Karger44cf68f2013-11-12 10:34:55 +010090static inline void sha2_hmac_starts( sha256_context *ctx, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020091 size_t keylen, int is224 ) {
92 sha256_hmac_starts( ctx, key, keylen, is224 );
93}
Steffan Karger44cf68f2013-11-12 10:34:55 +010094static inline void sha2_hmac_update( sha256_context *ctx, const unsigned char *input, size_t ilen ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020095 sha256_hmac_update( ctx, input, ilen );
96}
Steffan Karger44cf68f2013-11-12 10:34:55 +010097static inline void sha2_hmac_finish( sha256_context *ctx, unsigned char output[32] ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020098 sha256_hmac_finish( ctx, output );
99}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100100static inline void sha2_hmac_reset( sha256_context *ctx ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200101 sha256_hmac_reset( ctx );
102}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100103static inline void sha2_hmac( const unsigned char *key, size_t keylen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200104 const unsigned char *input, size_t ilen,
105 unsigned char output[32], int is224 ) {
106 sha256_hmac( key, keylen, input, ilen, output, is224 );
107}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100108static inline int sha2_self_test( int verbose ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200109 return sha256_self_test( verbose );
110}
111#endif /* POLARSSL_SHA256_C */
112
113#if defined(POLARSSL_SHA512_C)
114#define POLARSSL_SHA4_C
115#include "sha512.h"
116
117/*
118 * SHA-4 -> SHA-512
119 */
120typedef sha512_context sha4_context;
121
Steffan Karger44cf68f2013-11-12 10:34:55 +0100122static inline void sha4_starts( sha512_context *ctx, int is384 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200123 sha512_starts( ctx, is384 );
124}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100125static inline void sha4_update( sha512_context *ctx, const unsigned char *input,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200126 size_t ilen ) {
127 sha512_update( ctx, input, ilen );
128}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100129static inline void sha4_finish( sha512_context *ctx, unsigned char output[64] ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200130 sha512_finish( ctx, output );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200131}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100132static inline int sha4_file( const char *path, unsigned char output[64], int is384 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200133 return sha512_file( path, output, is384 );
134}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100135static inline void sha4( const unsigned char *input, size_t ilen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200136 unsigned char output[32], int is384 ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200137 sha512( input, ilen, output, is384 );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200138}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100139static inline void sha4_hmac_starts( sha512_context *ctx, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200140 size_t keylen, int is384 ) {
141 sha512_hmac_starts( ctx, key, keylen, is384 );
142}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100143static inline void sha4_hmac_update( sha512_context *ctx, const unsigned char *input, size_t ilen ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200144 sha512_hmac_update( ctx, input, ilen );
145}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100146static inline void sha4_hmac_finish( sha512_context *ctx, unsigned char output[64] ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200147 sha512_hmac_finish( ctx, output );
148}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100149static inline void sha4_hmac_reset( sha512_context *ctx ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200150 sha512_hmac_reset( ctx );
151}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100152static inline void sha4_hmac( const unsigned char *key, size_t keylen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200153 const unsigned char *input, size_t ilen,
154 unsigned char output[64], int is384 ) {
155 sha512_hmac( key, keylen, input, ilen, output, is384 );
156}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100157static inline int sha4_self_test( int verbose ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200158 return sha512_self_test( verbose );
159}
160#endif /* POLARSSL_SHA512_C */
161
162#if defined(POLARSSL_CIPHER_C)
163#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
164#warning "cipher_reset() prototype changed. Manual change required if used"
165#endif
166#endif
167
168#if defined(POLARSSL_RSA_C)
169#define SIG_RSA_RAW POLARSSL_MD_NONE
170#define SIG_RSA_MD2 POLARSSL_MD_MD2
171#define SIG_RSA_MD4 POLARSSL_MD_MD4
172#define SIG_RSA_MD5 POLARSSL_MD_MD5
173#define SIG_RSA_SHA1 POLARSSL_MD_SHA1
174#define SIG_RSA_SHA224 POLARSSL_MD_SHA224
175#define SIG_RSA_SHA256 POLARSSL_MD_SHA256
176#define SIG_RSA_SHA384 POLARSSL_MD_SHA384
177#define SIG_RSA_SHA512 POLARSSL_MD_SHA512
178#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
179#warning "rsa_pkcs1_verify() prototype changed. Manual change required if used"
180#warning "rsa_pkcs1_decrypt() prototype changed. Manual change required if used"
181#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200182#endif /* POLARSSL_RSA_C */
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200183
184#if defined(POLARSSL_DHM_C)
185#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
186#warning "dhm_calc_secret() prototype changed. Manual change required if used"
187#endif
188#endif
189
190#if defined(POLARSSL_GCM_C)
191#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
192#warning "gcm_init() prototype changed. Manual change required if used"
193#endif
194#endif
195
196#if defined(POLARSSL_SSL_CLI_C)
197#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
198#warning "ssl_set_own_cert() prototype changed. Change to ssl_set_own_cert_rsa(). Manual change required if used"
199#endif
200#endif
201
Paul Bakker51876562013-09-17 14:36:05 +0200202#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakker86d0c192013-09-18 11:11:02 +0200203#include "x509.h"
204
Paul Bakker51876562013-09-17 14:36:05 +0200205#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT POLARSSL_ERR_X509_INVALID_FORMAT
206#define POLARSSL_ERR_X509_CERT_INVALID_VERSION POLARSSL_ERR_X509_INVALID_VERSION
207#define POLARSSL_ERR_X509_CERT_INVALID_ALG POLARSSL_ERR_X509_INVALID_ALG
208#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG POLARSSL_ERR_X509_UNKNOWN_SIG_ALG
209#define POLARSSL_ERR_X509_CERT_INVALID_NAME POLARSSL_ERR_X509_INVALID_NAME
210#define POLARSSL_ERR_X509_CERT_INVALID_DATE POLARSSL_ERR_X509_INVALID_DATE
211#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS POLARSSL_ERR_X509_INVALID_EXTENSIONS
212#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH POLARSSL_ERR_X509_SIG_MISMATCH
213#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE POLARSSL_ERR_X509_INVALID_SIGNATURE
214#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL POLARSSL_ERR_X509_INVALID_SERIAL
215#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION POLARSSL_ERR_X509_UNKNOWN_VERSION
Paul Bakker86d0c192013-09-18 11:11:02 +0200216
Steffan Karger44cf68f2013-11-12 10:34:55 +0100217static inline int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200218 return x509_serial_gets( buf, size, serial );
219}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100220static inline int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200221 return x509_dn_gets( buf, size, dn );
222}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100223static inline int x509parse_time_expired( const x509_time *time ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200224 return x509_time_expired( time );
225}
Paul Bakker51876562013-09-17 14:36:05 +0200226#endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
227
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200228#if defined(POLARSSL_X509_CRT_PARSE_C)
229#define POLARSSL_X509_PARSE_C
230#include "x509_crt.h"
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200231typedef x509_crt x509_cert;
232
Steffan Karger44cf68f2013-11-12 10:34:55 +0100233static inline int x509parse_crt_der( x509_cert *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200234 size_t buflen ) {
235 return x509_crt_parse_der( chain, buf, buflen );
236}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100237static inline int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200238 return x509_crt_parse( chain, buf, buflen );
239}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100240static inline int x509parse_crtfile( x509_cert *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200241 return x509_crt_parse_file( chain, path );
242}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100243static inline int x509parse_crtpath( x509_cert *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200244 return x509_crt_parse_path( chain, path );
245}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100246static inline int x509parse_cert_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200247 const x509_cert *crt ) {
248 return x509_crt_info( buf, size, prefix, crt );
249}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100250static inline int x509parse_verify( x509_cert *crt, x509_cert *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200251 x509_crl *ca_crl, const char *cn, int *flags,
252 int (*f_vrfy)(void *, x509_cert *, int, int *),
253 void *p_vrfy ) {
254 return x509_crt_verify( crt, trust_ca, ca_crl, cn, flags, f_vrfy, p_vrfy );
255}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100256static inline int x509parse_revoked( const x509_cert *crt, const x509_crl *crl ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200257 return x509_crt_revoked( crt, crl );
258}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100259static inline void x509_free( x509_cert *crt ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200260 x509_crt_free( crt );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200261}
262#endif /* POLARSSL_X509_CRT_PARSE_C */
263
Paul Bakkerddf26b42013-09-18 13:46:23 +0200264#if defined(POLARSSL_X509_CRL_PARSE_C)
265#define POLARSSL_X509_PARSE_C
266#include "x509_crl.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100267static inline int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200268 return x509_crl_parse( chain, buf, buflen );
269}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100270static inline int x509parse_crlfile( x509_crl *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200271 return x509_crl_parse_file( chain, path );
272}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100273static inline int x509parse_crl_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200274 const x509_crl *crl ) {
275 return x509_crl_info( buf, size, prefix, crl );
276}
277#endif /* POLARSSL_X509_CRL_PARSE_C */
278
279#if defined(POLARSSL_X509_CSR_PARSE_C)
280#define POLARSSL_X509_PARSE_C
281#include "x509_csr.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100282static inline int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200283 return x509_csr_parse( csr, buf, buflen );
284}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100285static inline int x509parse_csrfile( x509_csr *csr, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200286 return x509_csr_parse_file( csr, path );
287}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100288static inline int x509parse_csr_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200289 const x509_csr *csr ) {
290 return x509_csr_info( buf, size, prefix, csr );
291}
292#endif /* POLARSSL_X509_CSR_PARSE_C */
293
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200294#if defined(POLARSSL_SSL_TLS_C)
295#include "ssl_ciphersuites.h"
296
297#define ssl_default_ciphersuites ssl_list_ciphersuites()
298#endif
299
300#if defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_RSA_C)
301#include "rsa.h"
302#include "pk.h"
303
304#define POLARSSL_ERR_X509_PASSWORD_MISMATCH POLARSSL_ERR_PK_PASSWORD_MISMATCH
305#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT POLARSSL_ERR_PK_KEY_INVALID_FORMAT
306#define POLARSSL_ERR_X509_UNKNOWN_PK_ALG POLARSSL_ERR_PK_UNKNOWN_PK_ALG
307#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY POLARSSL_ERR_PK_INVALID_PUBKEY
308
309#if defined(POLARSSL_FS_IO)
Steffan Karger44cf68f2013-11-12 10:34:55 +0100310static inline int x509parse_keyfile( rsa_context *rsa, const char *path,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200311 const char *pwd ) {
312 int ret;
313 pk_context pk;
314 pk_init( &pk );
315 ret = pk_parse_keyfile( &pk, path, pwd );
316 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
317 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
318 if( ret == 0 )
319 rsa_copy( rsa, pk_rsa( pk ) );
320 else
321 rsa_free( rsa );
322 pk_free( &pk );
323 return( ret );
324}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100325static inline int x509parse_public_keyfile( rsa_context *rsa, const char *path ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200326 int ret;
327 pk_context pk;
328 pk_init( &pk );
329 ret = pk_parse_public_keyfile( &pk, path );
330 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
331 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
332 if( ret == 0 )
333 rsa_copy( rsa, pk_rsa( pk ) );
334 else
335 rsa_free( rsa );
336 pk_free( &pk );
337 return( ret );
338}
339#endif /* POLARSSL_FS_IO */
340
Steffan Karger44cf68f2013-11-12 10:34:55 +0100341static inline int x509parse_key( rsa_context *rsa, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200342 size_t keylen,
343 const unsigned char *pwd, size_t pwdlen ) {
344 int ret;
345 pk_context pk;
346 pk_init( &pk );
347 ret = pk_parse_key( &pk, key, keylen, pwd, pwdlen );
348 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
349 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
350 if( ret == 0 )
351 rsa_copy( rsa, pk_rsa( pk ) );
352 else
353 rsa_free( rsa );
354 pk_free( &pk );
355 return( ret );
356}
357
Steffan Karger44cf68f2013-11-12 10:34:55 +0100358static inline int x509parse_public_key( rsa_context *rsa,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200359 const unsigned char *key, size_t keylen )
360{
361 int ret;
362 pk_context pk;
363 pk_init( &pk );
364 ret = pk_parse_public_key( &pk, key, keylen );
365 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
366 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
367 if( ret == 0 )
368 rsa_copy( rsa, pk_rsa( pk ) );
369 else
370 rsa_free( rsa );
371 pk_free( &pk );
372 return( ret );
373}
374#endif /* POLARSSL_PK_PARSE_C && POLARSSL_RSA_C */
375
376#if defined(POLARSSL_PK_WRITE_C) && defined(POLARSSL_RSA_C)
377#include "pk.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100378static inline int x509_write_pubkey_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200379 int ret;
380 pk_context ctx;
381 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200382 if( ( ret = rsa_copy( pk_rsa( ctx ), rsa ) ) != 0 ) return( ret );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200383 ret = pk_write_pubkey_der( &ctx, buf, len );
384 pk_free( &ctx );
385 return( ret );
386}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100387static inline int x509_write_key_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200388 int ret;
389 pk_context ctx;
390 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200391 if( ( ret = rsa_copy( pk_rsa( ctx ), rsa ) ) != 0 ) return( ret );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200392 ret = pk_write_key_der( &ctx, buf, len );
393 pk_free( &ctx );
394 return( ret );
395}
396#endif /* POLARSSL_PK_WRITE_C && POLARSSL_RSA_C */
397#endif /* compat-1.2.h */
Manuel Pégourié-Gonnard8c3f0f42015-04-09 14:10:26 +0200398#endif /* MBEDTLS_DEPRECATED_REMOVED */