blob: bdc506ad910458f4085d2296976da5b8a40ca2cd [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 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +00008 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakkerd1a983f2013-09-16 22:26:53 +02009 * 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerd1a983f2013-09-16 22:26:53 +020031#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakkerd1a983f2013-09-16 22:26:53 +020035
Paul Bakker51876562013-09-17 14:36:05 +020036// Comment out to disable prototype change warnings
Paul Bakkerd1a983f2013-09-16 22:26:53 +020037#define SHOW_PROTOTYPE_CHANGE_WARNINGS
38
Paul Bakkerfa6a6202013-10-28 18:48:30 +010039#if defined(_MSC_VER) && !defined(inline)
40#define inline _inline
41#else
42#if defined(__ARMCC_VERSION) && !defined(inline)
43#define inline __inline
44#endif /* __ARMCC_VERSION */
Paul Bakkerd46a9f12013-10-31 14:34:19 +010045#endif /* _MSC_VER */
Paul Bakkerfa6a6202013-10-28 18:48:30 +010046
47#if defined(_MSC_VER)
Paul Bakker4aa40d42013-10-11 10:49:24 +020048// MSVC does not support #warning
49#undef SHOW_PROTOTYPE_CHANGE_WARNINGS
50#endif
51
Paul Bakker51876562013-09-17 14:36:05 +020052#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
53#warning "You can disable these warnings by commenting SHOW_PROTOTYPE_CHANGE_WARNINGS in compat-1.2.h"
54#endif
55
Paul Bakkerd1a983f2013-09-16 22:26:53 +020056#if defined(POLARSSL_SHA256_C)
57#define POLARSSL_SHA2_C
58#include "sha256.h"
59
60/*
61 * SHA-2 -> SHA-256
62 */
63typedef sha256_context sha2_context;
64
Steffan Karger44cf68f2013-11-12 10:34:55 +010065static inline void sha2_starts( sha256_context *ctx, int is224 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020066 sha256_starts( ctx, is224 );
67}
Steffan Karger44cf68f2013-11-12 10:34:55 +010068static inline void sha2_update( sha256_context *ctx, const unsigned char *input,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020069 size_t ilen ) {
70 sha256_update( ctx, input, ilen );
71}
Steffan Karger44cf68f2013-11-12 10:34:55 +010072static inline void sha2_finish( sha256_context *ctx, unsigned char output[32] ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +020073 sha256_finish( ctx, output );
Paul Bakkerd1a983f2013-09-16 22:26:53 +020074}
Steffan Karger44cf68f2013-11-12 10:34:55 +010075static inline int sha2_file( const char *path, unsigned char output[32], int is224 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020076 return sha256_file( path, output, is224 );
77}
Steffan Karger44cf68f2013-11-12 10:34:55 +010078static inline void sha2( const unsigned char *input, size_t ilen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020079 unsigned char output[32], int is224 ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +020080 sha256( input, ilen, output, is224 );
Paul Bakkerd1a983f2013-09-16 22:26:53 +020081}
Steffan Karger44cf68f2013-11-12 10:34:55 +010082static inline void sha2_hmac_starts( sha256_context *ctx, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020083 size_t keylen, int is224 ) {
84 sha256_hmac_starts( ctx, key, keylen, is224 );
85}
Steffan Karger44cf68f2013-11-12 10:34:55 +010086static inline void sha2_hmac_update( sha256_context *ctx, const unsigned char *input, size_t ilen ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020087 sha256_hmac_update( ctx, input, ilen );
88}
Steffan Karger44cf68f2013-11-12 10:34:55 +010089static inline void sha2_hmac_finish( sha256_context *ctx, unsigned char output[32] ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020090 sha256_hmac_finish( ctx, output );
91}
Steffan Karger44cf68f2013-11-12 10:34:55 +010092static inline void sha2_hmac_reset( sha256_context *ctx ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020093 sha256_hmac_reset( ctx );
94}
Steffan Karger44cf68f2013-11-12 10:34:55 +010095static inline void sha2_hmac( const unsigned char *key, size_t keylen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020096 const unsigned char *input, size_t ilen,
97 unsigned char output[32], int is224 ) {
98 sha256_hmac( key, keylen, input, ilen, output, is224 );
99}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100100static inline int sha2_self_test( int verbose ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200101 return sha256_self_test( verbose );
102}
103#endif /* POLARSSL_SHA256_C */
104
105#if defined(POLARSSL_SHA512_C)
106#define POLARSSL_SHA4_C
107#include "sha512.h"
108
109/*
110 * SHA-4 -> SHA-512
111 */
112typedef sha512_context sha4_context;
113
Steffan Karger44cf68f2013-11-12 10:34:55 +0100114static inline void sha4_starts( sha512_context *ctx, int is384 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200115 sha512_starts( ctx, is384 );
116}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100117static inline void sha4_update( sha512_context *ctx, const unsigned char *input,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200118 size_t ilen ) {
119 sha512_update( ctx, input, ilen );
120}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100121static inline void sha4_finish( sha512_context *ctx, unsigned char output[64] ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200122 sha512_finish( ctx, output );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200123}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100124static inline int sha4_file( const char *path, unsigned char output[64], int is384 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200125 return sha512_file( path, output, is384 );
126}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100127static inline void sha4( const unsigned char *input, size_t ilen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200128 unsigned char output[32], int is384 ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200129 sha512( input, ilen, output, is384 );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200130}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100131static inline void sha4_hmac_starts( sha512_context *ctx, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200132 size_t keylen, int is384 ) {
133 sha512_hmac_starts( ctx, key, keylen, is384 );
134}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100135static inline void sha4_hmac_update( sha512_context *ctx, const unsigned char *input, size_t ilen ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200136 sha512_hmac_update( ctx, input, ilen );
137}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100138static inline void sha4_hmac_finish( sha512_context *ctx, unsigned char output[64] ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200139 sha512_hmac_finish( ctx, output );
140}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100141static inline void sha4_hmac_reset( sha512_context *ctx ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200142 sha512_hmac_reset( ctx );
143}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100144static inline void sha4_hmac( const unsigned char *key, size_t keylen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200145 const unsigned char *input, size_t ilen,
146 unsigned char output[64], int is384 ) {
147 sha512_hmac( key, keylen, input, ilen, output, is384 );
148}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100149static inline int sha4_self_test( int verbose ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200150 return sha512_self_test( verbose );
151}
152#endif /* POLARSSL_SHA512_C */
153
154#if defined(POLARSSL_CIPHER_C)
155#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
156#warning "cipher_reset() prototype changed. Manual change required if used"
157#endif
158#endif
159
160#if defined(POLARSSL_RSA_C)
161#define SIG_RSA_RAW POLARSSL_MD_NONE
162#define SIG_RSA_MD2 POLARSSL_MD_MD2
163#define SIG_RSA_MD4 POLARSSL_MD_MD4
164#define SIG_RSA_MD5 POLARSSL_MD_MD5
165#define SIG_RSA_SHA1 POLARSSL_MD_SHA1
166#define SIG_RSA_SHA224 POLARSSL_MD_SHA224
167#define SIG_RSA_SHA256 POLARSSL_MD_SHA256
168#define SIG_RSA_SHA384 POLARSSL_MD_SHA384
169#define SIG_RSA_SHA512 POLARSSL_MD_SHA512
170#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
171#warning "rsa_pkcs1_verify() prototype changed. Manual change required if used"
172#warning "rsa_pkcs1_decrypt() prototype changed. Manual change required if used"
173#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200174#endif /* POLARSSL_RSA_C */
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200175
176#if defined(POLARSSL_DHM_C)
177#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
178#warning "dhm_calc_secret() prototype changed. Manual change required if used"
179#endif
180#endif
181
182#if defined(POLARSSL_GCM_C)
183#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
184#warning "gcm_init() prototype changed. Manual change required if used"
185#endif
186#endif
187
188#if defined(POLARSSL_SSL_CLI_C)
189#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
190#warning "ssl_set_own_cert() prototype changed. Change to ssl_set_own_cert_rsa(). Manual change required if used"
191#endif
192#endif
193
Paul Bakker51876562013-09-17 14:36:05 +0200194#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakker86d0c192013-09-18 11:11:02 +0200195#include "x509.h"
196
Paul Bakker51876562013-09-17 14:36:05 +0200197#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT POLARSSL_ERR_X509_INVALID_FORMAT
198#define POLARSSL_ERR_X509_CERT_INVALID_VERSION POLARSSL_ERR_X509_INVALID_VERSION
199#define POLARSSL_ERR_X509_CERT_INVALID_ALG POLARSSL_ERR_X509_INVALID_ALG
200#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG POLARSSL_ERR_X509_UNKNOWN_SIG_ALG
201#define POLARSSL_ERR_X509_CERT_INVALID_NAME POLARSSL_ERR_X509_INVALID_NAME
202#define POLARSSL_ERR_X509_CERT_INVALID_DATE POLARSSL_ERR_X509_INVALID_DATE
203#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS POLARSSL_ERR_X509_INVALID_EXTENSIONS
204#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH POLARSSL_ERR_X509_SIG_MISMATCH
205#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE POLARSSL_ERR_X509_INVALID_SIGNATURE
206#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL POLARSSL_ERR_X509_INVALID_SERIAL
207#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION POLARSSL_ERR_X509_UNKNOWN_VERSION
Paul Bakker86d0c192013-09-18 11:11:02 +0200208
Steffan Karger44cf68f2013-11-12 10:34:55 +0100209static inline int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200210 return x509_serial_gets( buf, size, serial );
211}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100212static inline int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200213 return x509_dn_gets( buf, size, dn );
214}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100215static inline int x509parse_time_expired( const x509_time *time ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200216 return x509_time_expired( time );
217}
Paul Bakker51876562013-09-17 14:36:05 +0200218#endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
219
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200220#if defined(POLARSSL_X509_CRT_PARSE_C)
221#define POLARSSL_X509_PARSE_C
222#include "x509_crt.h"
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200223typedef x509_crt x509_cert;
224
Steffan Karger44cf68f2013-11-12 10:34:55 +0100225static inline int x509parse_crt_der( x509_cert *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200226 size_t buflen ) {
227 return x509_crt_parse_der( chain, buf, buflen );
228}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100229static inline int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200230 return x509_crt_parse( chain, buf, buflen );
231}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100232static inline int x509parse_crtfile( x509_cert *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200233 return x509_crt_parse_file( chain, path );
234}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100235static inline int x509parse_crtpath( x509_cert *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200236 return x509_crt_parse_path( chain, path );
237}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100238static inline int x509parse_cert_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200239 const x509_cert *crt ) {
240 return x509_crt_info( buf, size, prefix, crt );
241}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100242static inline int x509parse_verify( x509_cert *crt, x509_cert *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200243 x509_crl *ca_crl, const char *cn, int *flags,
244 int (*f_vrfy)(void *, x509_cert *, int, int *),
245 void *p_vrfy ) {
246 return x509_crt_verify( crt, trust_ca, ca_crl, cn, flags, f_vrfy, p_vrfy );
247}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100248static inline int x509parse_revoked( const x509_cert *crt, const x509_crl *crl ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200249 return x509_crt_revoked( crt, crl );
250}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100251static inline void x509_free( x509_cert *crt ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200252 x509_crt_free( crt );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200253}
254#endif /* POLARSSL_X509_CRT_PARSE_C */
255
Paul Bakkerddf26b42013-09-18 13:46:23 +0200256#if defined(POLARSSL_X509_CRL_PARSE_C)
257#define POLARSSL_X509_PARSE_C
258#include "x509_crl.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100259static inline int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200260 return x509_crl_parse( chain, buf, buflen );
261}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100262static inline int x509parse_crlfile( x509_crl *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200263 return x509_crl_parse_file( chain, path );
264}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100265static inline int x509parse_crl_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200266 const x509_crl *crl ) {
267 return x509_crl_info( buf, size, prefix, crl );
268}
269#endif /* POLARSSL_X509_CRL_PARSE_C */
270
271#if defined(POLARSSL_X509_CSR_PARSE_C)
272#define POLARSSL_X509_PARSE_C
273#include "x509_csr.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100274static inline int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200275 return x509_csr_parse( csr, buf, buflen );
276}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100277static inline int x509parse_csrfile( x509_csr *csr, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200278 return x509_csr_parse_file( csr, path );
279}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100280static inline int x509parse_csr_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200281 const x509_csr *csr ) {
282 return x509_csr_info( buf, size, prefix, csr );
283}
284#endif /* POLARSSL_X509_CSR_PARSE_C */
285
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200286#if defined(POLARSSL_SSL_TLS_C)
287#include "ssl_ciphersuites.h"
288
289#define ssl_default_ciphersuites ssl_list_ciphersuites()
290#endif
291
292#if defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_RSA_C)
293#include "rsa.h"
294#include "pk.h"
295
296#define POLARSSL_ERR_X509_PASSWORD_MISMATCH POLARSSL_ERR_PK_PASSWORD_MISMATCH
297#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT POLARSSL_ERR_PK_KEY_INVALID_FORMAT
298#define POLARSSL_ERR_X509_UNKNOWN_PK_ALG POLARSSL_ERR_PK_UNKNOWN_PK_ALG
299#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY POLARSSL_ERR_PK_INVALID_PUBKEY
300
301#if defined(POLARSSL_FS_IO)
Steffan Karger44cf68f2013-11-12 10:34:55 +0100302static inline int x509parse_keyfile( rsa_context *rsa, const char *path,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200303 const char *pwd ) {
304 int ret;
305 pk_context pk;
306 pk_init( &pk );
307 ret = pk_parse_keyfile( &pk, path, pwd );
308 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
309 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
310 if( ret == 0 )
311 rsa_copy( rsa, pk_rsa( pk ) );
312 else
313 rsa_free( rsa );
314 pk_free( &pk );
315 return( ret );
316}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100317static inline int x509parse_public_keyfile( rsa_context *rsa, const char *path ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200318 int ret;
319 pk_context pk;
320 pk_init( &pk );
321 ret = pk_parse_public_keyfile( &pk, path );
322 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
323 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
324 if( ret == 0 )
325 rsa_copy( rsa, pk_rsa( pk ) );
326 else
327 rsa_free( rsa );
328 pk_free( &pk );
329 return( ret );
330}
331#endif /* POLARSSL_FS_IO */
332
Steffan Karger44cf68f2013-11-12 10:34:55 +0100333static inline int x509parse_key( rsa_context *rsa, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200334 size_t keylen,
335 const unsigned char *pwd, size_t pwdlen ) {
336 int ret;
337 pk_context pk;
338 pk_init( &pk );
339 ret = pk_parse_key( &pk, key, keylen, pwd, pwdlen );
340 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
341 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
342 if( ret == 0 )
343 rsa_copy( rsa, pk_rsa( pk ) );
344 else
345 rsa_free( rsa );
346 pk_free( &pk );
347 return( ret );
348}
349
Steffan Karger44cf68f2013-11-12 10:34:55 +0100350static inline int x509parse_public_key( rsa_context *rsa,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200351 const unsigned char *key, size_t keylen )
352{
353 int ret;
354 pk_context pk;
355 pk_init( &pk );
356 ret = pk_parse_public_key( &pk, key, keylen );
357 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
358 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
359 if( ret == 0 )
360 rsa_copy( rsa, pk_rsa( pk ) );
361 else
362 rsa_free( rsa );
363 pk_free( &pk );
364 return( ret );
365}
366#endif /* POLARSSL_PK_PARSE_C && POLARSSL_RSA_C */
367
368#if defined(POLARSSL_PK_WRITE_C) && defined(POLARSSL_RSA_C)
369#include "pk.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100370static inline int x509_write_pubkey_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200371 int ret;
372 pk_context ctx;
373 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200374 if( ( ret = rsa_copy( pk_rsa( ctx ), rsa ) ) != 0 ) return( ret );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200375 ret = pk_write_pubkey_der( &ctx, buf, len );
376 pk_free( &ctx );
377 return( ret );
378}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100379static inline int x509_write_key_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200380 int ret;
381 pk_context ctx;
382 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200383 if( ( ret = rsa_copy( pk_rsa( ctx ), rsa ) ) != 0 ) return( ret );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200384 ret = pk_write_key_der( &ctx, buf, len );
385 pk_free( &ctx );
386 return( ret );
387}
388#endif /* POLARSSL_PK_WRITE_C && POLARSSL_RSA_C */
389#endif /* compat-1.2.h */