blob: 47a4516793f93f18eafcc95129952062ff4e8bb2 [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é-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerd1a983f2013-09-16 22:26:53 +02007 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerd1a983f2013-09-16 22:26:53 +02009 *
Paul Bakkerd1a983f2013-09-16 22:26:53 +020010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24#ifndef POLARSSL_COMPAT_1_2_H
25#define POLARSSL_COMPAT_1_2_H
26
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerd1a983f2013-09-16 22:26:53 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakkerd1a983f2013-09-16 22:26:53 +020032
Paul Bakker51876562013-09-17 14:36:05 +020033// Comment out to disable prototype change warnings
Paul Bakkerd1a983f2013-09-16 22:26:53 +020034#define SHOW_PROTOTYPE_CHANGE_WARNINGS
35
Paul Bakkerfa6a6202013-10-28 18:48:30 +010036#if defined(_MSC_VER) && !defined(inline)
37#define inline _inline
38#else
39#if defined(__ARMCC_VERSION) && !defined(inline)
40#define inline __inline
41#endif /* __ARMCC_VERSION */
Paul Bakkerd46a9f12013-10-31 14:34:19 +010042#endif /* _MSC_VER */
Paul Bakkerfa6a6202013-10-28 18:48:30 +010043
44#if defined(_MSC_VER)
Paul Bakker4aa40d42013-10-11 10:49:24 +020045// MSVC does not support #warning
46#undef SHOW_PROTOTYPE_CHANGE_WARNINGS
47#endif
48
Paul Bakker51876562013-09-17 14:36:05 +020049#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
50#warning "You can disable these warnings by commenting SHOW_PROTOTYPE_CHANGE_WARNINGS in compat-1.2.h"
51#endif
52
Paul Bakkerd1a983f2013-09-16 22:26:53 +020053#if defined(POLARSSL_SHA256_C)
54#define POLARSSL_SHA2_C
55#include "sha256.h"
56
57/*
58 * SHA-2 -> SHA-256
59 */
60typedef sha256_context sha2_context;
61
Steffan Karger44cf68f2013-11-12 10:34:55 +010062static inline void sha2_starts( sha256_context *ctx, int is224 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020063 sha256_starts( ctx, is224 );
64}
Steffan Karger44cf68f2013-11-12 10:34:55 +010065static inline void sha2_update( sha256_context *ctx, const unsigned char *input,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020066 size_t ilen ) {
67 sha256_update( ctx, input, ilen );
68}
Steffan Karger44cf68f2013-11-12 10:34:55 +010069static inline void sha2_finish( sha256_context *ctx, unsigned char output[32] ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +020070 sha256_finish( ctx, output );
Paul Bakkerd1a983f2013-09-16 22:26:53 +020071}
Steffan Karger44cf68f2013-11-12 10:34:55 +010072static inline int sha2_file( const char *path, unsigned char output[32], int is224 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020073 return sha256_file( path, output, is224 );
74}
Steffan Karger44cf68f2013-11-12 10:34:55 +010075static inline void sha2( const unsigned char *input, size_t ilen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020076 unsigned char output[32], int is224 ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +020077 sha256( input, ilen, output, is224 );
Paul Bakkerd1a983f2013-09-16 22:26:53 +020078}
Steffan Karger44cf68f2013-11-12 10:34:55 +010079static inline void sha2_hmac_starts( sha256_context *ctx, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020080 size_t keylen, int is224 ) {
81 sha256_hmac_starts( ctx, key, keylen, is224 );
82}
Steffan Karger44cf68f2013-11-12 10:34:55 +010083static inline void sha2_hmac_update( sha256_context *ctx, const unsigned char *input, size_t ilen ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020084 sha256_hmac_update( ctx, input, ilen );
85}
Steffan Karger44cf68f2013-11-12 10:34:55 +010086static inline void sha2_hmac_finish( sha256_context *ctx, unsigned char output[32] ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020087 sha256_hmac_finish( ctx, output );
88}
Steffan Karger44cf68f2013-11-12 10:34:55 +010089static inline void sha2_hmac_reset( sha256_context *ctx ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020090 sha256_hmac_reset( ctx );
91}
Steffan Karger44cf68f2013-11-12 10:34:55 +010092static inline void sha2_hmac( const unsigned char *key, size_t keylen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020093 const unsigned char *input, size_t ilen,
94 unsigned char output[32], int is224 ) {
95 sha256_hmac( key, keylen, input, ilen, output, is224 );
96}
Steffan Karger44cf68f2013-11-12 10:34:55 +010097static inline int sha2_self_test( int verbose ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020098 return sha256_self_test( verbose );
99}
100#endif /* POLARSSL_SHA256_C */
101
102#if defined(POLARSSL_SHA512_C)
103#define POLARSSL_SHA4_C
104#include "sha512.h"
105
106/*
107 * SHA-4 -> SHA-512
108 */
109typedef sha512_context sha4_context;
110
Steffan Karger44cf68f2013-11-12 10:34:55 +0100111static inline void sha4_starts( sha512_context *ctx, int is384 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200112 sha512_starts( ctx, is384 );
113}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100114static inline void sha4_update( sha512_context *ctx, const unsigned char *input,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200115 size_t ilen ) {
116 sha512_update( ctx, input, ilen );
117}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100118static inline void sha4_finish( sha512_context *ctx, unsigned char output[64] ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200119 sha512_finish( ctx, output );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200120}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100121static inline int sha4_file( const char *path, unsigned char output[64], int is384 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200122 return sha512_file( path, output, is384 );
123}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100124static inline void sha4( const unsigned char *input, size_t ilen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200125 unsigned char output[32], int is384 ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200126 sha512( input, ilen, output, is384 );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200127}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100128static inline void sha4_hmac_starts( sha512_context *ctx, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200129 size_t keylen, int is384 ) {
130 sha512_hmac_starts( ctx, key, keylen, is384 );
131}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100132static inline void sha4_hmac_update( sha512_context *ctx, const unsigned char *input, size_t ilen ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200133 sha512_hmac_update( ctx, input, ilen );
134}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100135static inline void sha4_hmac_finish( sha512_context *ctx, unsigned char output[64] ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200136 sha512_hmac_finish( ctx, output );
137}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100138static inline void sha4_hmac_reset( sha512_context *ctx ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200139 sha512_hmac_reset( ctx );
140}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100141static inline void sha4_hmac( const unsigned char *key, size_t keylen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200142 const unsigned char *input, size_t ilen,
143 unsigned char output[64], int is384 ) {
144 sha512_hmac( key, keylen, input, ilen, output, is384 );
145}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100146static inline int sha4_self_test( int verbose ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200147 return sha512_self_test( verbose );
148}
149#endif /* POLARSSL_SHA512_C */
150
151#if defined(POLARSSL_CIPHER_C)
152#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
153#warning "cipher_reset() prototype changed. Manual change required if used"
154#endif
155#endif
156
157#if defined(POLARSSL_RSA_C)
158#define SIG_RSA_RAW POLARSSL_MD_NONE
159#define SIG_RSA_MD2 POLARSSL_MD_MD2
160#define SIG_RSA_MD4 POLARSSL_MD_MD4
161#define SIG_RSA_MD5 POLARSSL_MD_MD5
162#define SIG_RSA_SHA1 POLARSSL_MD_SHA1
163#define SIG_RSA_SHA224 POLARSSL_MD_SHA224
164#define SIG_RSA_SHA256 POLARSSL_MD_SHA256
165#define SIG_RSA_SHA384 POLARSSL_MD_SHA384
166#define SIG_RSA_SHA512 POLARSSL_MD_SHA512
167#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
168#warning "rsa_pkcs1_verify() prototype changed. Manual change required if used"
169#warning "rsa_pkcs1_decrypt() prototype changed. Manual change required if used"
170#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200171#endif /* POLARSSL_RSA_C */
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200172
173#if defined(POLARSSL_DHM_C)
174#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
175#warning "dhm_calc_secret() prototype changed. Manual change required if used"
176#endif
177#endif
178
179#if defined(POLARSSL_GCM_C)
180#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
181#warning "gcm_init() prototype changed. Manual change required if used"
182#endif
183#endif
184
185#if defined(POLARSSL_SSL_CLI_C)
186#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
187#warning "ssl_set_own_cert() prototype changed. Change to ssl_set_own_cert_rsa(). Manual change required if used"
188#endif
189#endif
190
Paul Bakker51876562013-09-17 14:36:05 +0200191#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakker86d0c192013-09-18 11:11:02 +0200192#include "x509.h"
193
Paul Bakker51876562013-09-17 14:36:05 +0200194#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT POLARSSL_ERR_X509_INVALID_FORMAT
195#define POLARSSL_ERR_X509_CERT_INVALID_VERSION POLARSSL_ERR_X509_INVALID_VERSION
196#define POLARSSL_ERR_X509_CERT_INVALID_ALG POLARSSL_ERR_X509_INVALID_ALG
197#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG POLARSSL_ERR_X509_UNKNOWN_SIG_ALG
198#define POLARSSL_ERR_X509_CERT_INVALID_NAME POLARSSL_ERR_X509_INVALID_NAME
199#define POLARSSL_ERR_X509_CERT_INVALID_DATE POLARSSL_ERR_X509_INVALID_DATE
200#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS POLARSSL_ERR_X509_INVALID_EXTENSIONS
201#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH POLARSSL_ERR_X509_SIG_MISMATCH
202#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE POLARSSL_ERR_X509_INVALID_SIGNATURE
203#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL POLARSSL_ERR_X509_INVALID_SERIAL
204#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION POLARSSL_ERR_X509_UNKNOWN_VERSION
Paul Bakker86d0c192013-09-18 11:11:02 +0200205
Steffan Karger44cf68f2013-11-12 10:34:55 +0100206static inline int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200207 return x509_serial_gets( buf, size, serial );
208}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100209static inline int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200210 return x509_dn_gets( buf, size, dn );
211}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100212static inline int x509parse_time_expired( const x509_time *time ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200213 return x509_time_expired( time );
214}
Paul Bakker51876562013-09-17 14:36:05 +0200215#endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
216
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200217#if defined(POLARSSL_X509_CRT_PARSE_C)
218#define POLARSSL_X509_PARSE_C
219#include "x509_crt.h"
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200220typedef x509_crt x509_cert;
221
Steffan Karger44cf68f2013-11-12 10:34:55 +0100222static inline int x509parse_crt_der( x509_cert *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200223 size_t buflen ) {
224 return x509_crt_parse_der( chain, buf, buflen );
225}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100226static inline int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200227 return x509_crt_parse( chain, buf, buflen );
228}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100229static inline int x509parse_crtfile( x509_cert *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200230 return x509_crt_parse_file( chain, path );
231}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100232static inline int x509parse_crtpath( x509_cert *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200233 return x509_crt_parse_path( chain, path );
234}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100235static inline int x509parse_cert_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200236 const x509_cert *crt ) {
237 return x509_crt_info( buf, size, prefix, crt );
238}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100239static inline int x509parse_verify( x509_cert *crt, x509_cert *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200240 x509_crl *ca_crl, const char *cn, int *flags,
241 int (*f_vrfy)(void *, x509_cert *, int, int *),
242 void *p_vrfy ) {
243 return x509_crt_verify( crt, trust_ca, ca_crl, cn, flags, f_vrfy, p_vrfy );
244}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100245static inline int x509parse_revoked( const x509_cert *crt, const x509_crl *crl ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200246 return x509_crt_revoked( crt, crl );
247}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100248static inline void x509_free( x509_cert *crt ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200249 x509_crt_free( crt );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200250}
251#endif /* POLARSSL_X509_CRT_PARSE_C */
252
Paul Bakkerddf26b42013-09-18 13:46:23 +0200253#if defined(POLARSSL_X509_CRL_PARSE_C)
254#define POLARSSL_X509_PARSE_C
255#include "x509_crl.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100256static inline int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200257 return x509_crl_parse( chain, buf, buflen );
258}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100259static inline int x509parse_crlfile( x509_crl *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200260 return x509_crl_parse_file( chain, path );
261}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100262static inline int x509parse_crl_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200263 const x509_crl *crl ) {
264 return x509_crl_info( buf, size, prefix, crl );
265}
266#endif /* POLARSSL_X509_CRL_PARSE_C */
267
268#if defined(POLARSSL_X509_CSR_PARSE_C)
269#define POLARSSL_X509_PARSE_C
270#include "x509_csr.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100271static inline int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200272 return x509_csr_parse( csr, buf, buflen );
273}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100274static inline int x509parse_csrfile( x509_csr *csr, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200275 return x509_csr_parse_file( csr, path );
276}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100277static inline int x509parse_csr_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200278 const x509_csr *csr ) {
279 return x509_csr_info( buf, size, prefix, csr );
280}
281#endif /* POLARSSL_X509_CSR_PARSE_C */
282
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200283#if defined(POLARSSL_SSL_TLS_C)
284#include "ssl_ciphersuites.h"
285
286#define ssl_default_ciphersuites ssl_list_ciphersuites()
287#endif
288
289#if defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_RSA_C)
290#include "rsa.h"
291#include "pk.h"
292
293#define POLARSSL_ERR_X509_PASSWORD_MISMATCH POLARSSL_ERR_PK_PASSWORD_MISMATCH
294#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT POLARSSL_ERR_PK_KEY_INVALID_FORMAT
295#define POLARSSL_ERR_X509_UNKNOWN_PK_ALG POLARSSL_ERR_PK_UNKNOWN_PK_ALG
296#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY POLARSSL_ERR_PK_INVALID_PUBKEY
297
298#if defined(POLARSSL_FS_IO)
Steffan Karger44cf68f2013-11-12 10:34:55 +0100299static inline int x509parse_keyfile( rsa_context *rsa, const char *path,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200300 const char *pwd ) {
301 int ret;
302 pk_context pk;
303 pk_init( &pk );
304 ret = pk_parse_keyfile( &pk, path, pwd );
305 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
306 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
307 if( ret == 0 )
308 rsa_copy( rsa, pk_rsa( pk ) );
309 else
310 rsa_free( rsa );
311 pk_free( &pk );
312 return( ret );
313}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100314static inline int x509parse_public_keyfile( rsa_context *rsa, const char *path ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200315 int ret;
316 pk_context pk;
317 pk_init( &pk );
318 ret = pk_parse_public_keyfile( &pk, path );
319 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
320 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
321 if( ret == 0 )
322 rsa_copy( rsa, pk_rsa( pk ) );
323 else
324 rsa_free( rsa );
325 pk_free( &pk );
326 return( ret );
327}
328#endif /* POLARSSL_FS_IO */
329
Steffan Karger44cf68f2013-11-12 10:34:55 +0100330static inline int x509parse_key( rsa_context *rsa, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200331 size_t keylen,
332 const unsigned char *pwd, size_t pwdlen ) {
333 int ret;
334 pk_context pk;
335 pk_init( &pk );
336 ret = pk_parse_key( &pk, key, keylen, pwd, pwdlen );
337 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
338 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
339 if( ret == 0 )
340 rsa_copy( rsa, pk_rsa( pk ) );
341 else
342 rsa_free( rsa );
343 pk_free( &pk );
344 return( ret );
345}
346
Steffan Karger44cf68f2013-11-12 10:34:55 +0100347static inline int x509parse_public_key( rsa_context *rsa,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200348 const unsigned char *key, size_t keylen )
349{
350 int ret;
351 pk_context pk;
352 pk_init( &pk );
353 ret = pk_parse_public_key( &pk, key, keylen );
354 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
355 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
356 if( ret == 0 )
357 rsa_copy( rsa, pk_rsa( pk ) );
358 else
359 rsa_free( rsa );
360 pk_free( &pk );
361 return( ret );
362}
363#endif /* POLARSSL_PK_PARSE_C && POLARSSL_RSA_C */
364
365#if defined(POLARSSL_PK_WRITE_C) && defined(POLARSSL_RSA_C)
366#include "pk.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100367static inline int x509_write_pubkey_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200368 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_pubkey_der( &ctx, buf, len );
373 pk_free( &ctx );
374 return( ret );
375}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100376static inline int x509_write_key_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200377 int ret;
378 pk_context ctx;
379 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200380 if( ( ret = rsa_copy( pk_rsa( ctx ), rsa ) ) != 0 ) return( ret );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200381 ret = pk_write_key_der( &ctx, buf, len );
382 pk_free( &ctx );
383 return( ret );
384}
385#endif /* POLARSSL_PK_WRITE_C && POLARSSL_RSA_C */
386#endif /* compat-1.2.h */