blob: ca9a8e0682831981626ae77941a11d52a4e3907d [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 */
26#ifndef POLARSSL_COMPAT_1_2_H
27#define POLARSSL_COMPAT_1_2_H
28
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerd1a983f2013-09-16 22:26:53 +020030#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#else
32#include POLARSSL_CONFIG_FILE
33#endif
Paul Bakkerd1a983f2013-09-16 22:26:53 +020034
Paul Bakker51876562013-09-17 14:36:05 +020035// Comment out to disable prototype change warnings
Paul Bakkerd1a983f2013-09-16 22:26:53 +020036#define SHOW_PROTOTYPE_CHANGE_WARNINGS
37
Paul Bakkerfa6a6202013-10-28 18:48:30 +010038#if defined(_MSC_VER) && !defined(inline)
39#define inline _inline
40#else
41#if defined(__ARMCC_VERSION) && !defined(inline)
42#define inline __inline
43#endif /* __ARMCC_VERSION */
Paul Bakkerd46a9f12013-10-31 14:34:19 +010044#endif /* _MSC_VER */
Paul Bakkerfa6a6202013-10-28 18:48:30 +010045
46#if defined(_MSC_VER)
Paul Bakker4aa40d42013-10-11 10:49:24 +020047// MSVC does not support #warning
48#undef SHOW_PROTOTYPE_CHANGE_WARNINGS
49#endif
50
Paul Bakker51876562013-09-17 14:36:05 +020051#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
52#warning "You can disable these warnings by commenting SHOW_PROTOTYPE_CHANGE_WARNINGS in compat-1.2.h"
53#endif
54
Paul Bakkerd1a983f2013-09-16 22:26:53 +020055#if defined(POLARSSL_SHA256_C)
56#define POLARSSL_SHA2_C
57#include "sha256.h"
58
59/*
60 * SHA-2 -> SHA-256
61 */
62typedef sha256_context sha2_context;
63
Steffan Karger44cf68f2013-11-12 10:34:55 +010064static inline void sha2_starts( sha256_context *ctx, int is224 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020065 sha256_starts( ctx, is224 );
66}
Steffan Karger44cf68f2013-11-12 10:34:55 +010067static inline void sha2_update( sha256_context *ctx, const unsigned char *input,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020068 size_t ilen ) {
69 sha256_update( ctx, input, ilen );
70}
Steffan Karger44cf68f2013-11-12 10:34:55 +010071static inline void sha2_finish( sha256_context *ctx, unsigned char output[32] ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +020072 sha256_finish( ctx, output );
Paul Bakkerd1a983f2013-09-16 22:26:53 +020073}
Steffan Karger44cf68f2013-11-12 10:34:55 +010074static inline int sha2_file( const char *path, unsigned char output[32], int is224 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020075 return sha256_file( path, output, is224 );
76}
Steffan Karger44cf68f2013-11-12 10:34:55 +010077static inline void sha2( const unsigned char *input, size_t ilen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020078 unsigned char output[32], int is224 ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +020079 sha256( input, ilen, output, is224 );
Paul Bakkerd1a983f2013-09-16 22:26:53 +020080}
Steffan Karger44cf68f2013-11-12 10:34:55 +010081static inline void sha2_hmac_starts( sha256_context *ctx, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020082 size_t keylen, int is224 ) {
83 sha256_hmac_starts( ctx, key, keylen, is224 );
84}
Steffan Karger44cf68f2013-11-12 10:34:55 +010085static inline void sha2_hmac_update( sha256_context *ctx, const unsigned char *input, size_t ilen ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020086 sha256_hmac_update( ctx, input, ilen );
87}
Steffan Karger44cf68f2013-11-12 10:34:55 +010088static inline void sha2_hmac_finish( sha256_context *ctx, unsigned char output[32] ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020089 sha256_hmac_finish( ctx, output );
90}
Steffan Karger44cf68f2013-11-12 10:34:55 +010091static inline void sha2_hmac_reset( sha256_context *ctx ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +020092 sha256_hmac_reset( ctx );
93}
Steffan Karger44cf68f2013-11-12 10:34:55 +010094static inline void sha2_hmac( const unsigned char *key, size_t keylen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +020095 const unsigned char *input, size_t ilen,
96 unsigned char output[32], int is224 ) {
97 sha256_hmac( key, keylen, input, ilen, output, is224 );
98}
Steffan Karger44cf68f2013-11-12 10:34:55 +010099static inline int sha2_self_test( int verbose ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200100 return sha256_self_test( verbose );
101}
102#endif /* POLARSSL_SHA256_C */
103
104#if defined(POLARSSL_SHA512_C)
105#define POLARSSL_SHA4_C
106#include "sha512.h"
107
108/*
109 * SHA-4 -> SHA-512
110 */
111typedef sha512_context sha4_context;
112
Steffan Karger44cf68f2013-11-12 10:34:55 +0100113static inline void sha4_starts( sha512_context *ctx, int is384 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200114 sha512_starts( ctx, is384 );
115}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100116static inline void sha4_update( sha512_context *ctx, const unsigned char *input,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200117 size_t ilen ) {
118 sha512_update( ctx, input, ilen );
119}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100120static inline void sha4_finish( sha512_context *ctx, unsigned char output[64] ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200121 sha512_finish( ctx, output );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200122}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100123static inline int sha4_file( const char *path, unsigned char output[64], int is384 ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200124 return sha512_file( path, output, is384 );
125}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100126static inline void sha4( const unsigned char *input, size_t ilen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200127 unsigned char output[32], int is384 ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200128 sha512( input, ilen, output, is384 );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200129}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100130static inline void sha4_hmac_starts( sha512_context *ctx, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200131 size_t keylen, int is384 ) {
132 sha512_hmac_starts( ctx, key, keylen, is384 );
133}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100134static inline void sha4_hmac_update( sha512_context *ctx, const unsigned char *input, size_t ilen ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200135 sha512_hmac_update( ctx, input, ilen );
136}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100137static inline void sha4_hmac_finish( sha512_context *ctx, unsigned char output[64] ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200138 sha512_hmac_finish( ctx, output );
139}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100140static inline void sha4_hmac_reset( sha512_context *ctx ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200141 sha512_hmac_reset( ctx );
142}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100143static inline void sha4_hmac( const unsigned char *key, size_t keylen,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200144 const unsigned char *input, size_t ilen,
145 unsigned char output[64], int is384 ) {
146 sha512_hmac( key, keylen, input, ilen, output, is384 );
147}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100148static inline int sha4_self_test( int verbose ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200149 return sha512_self_test( verbose );
150}
151#endif /* POLARSSL_SHA512_C */
152
153#if defined(POLARSSL_CIPHER_C)
154#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
155#warning "cipher_reset() prototype changed. Manual change required if used"
156#endif
157#endif
158
159#if defined(POLARSSL_RSA_C)
160#define SIG_RSA_RAW POLARSSL_MD_NONE
161#define SIG_RSA_MD2 POLARSSL_MD_MD2
162#define SIG_RSA_MD4 POLARSSL_MD_MD4
163#define SIG_RSA_MD5 POLARSSL_MD_MD5
164#define SIG_RSA_SHA1 POLARSSL_MD_SHA1
165#define SIG_RSA_SHA224 POLARSSL_MD_SHA224
166#define SIG_RSA_SHA256 POLARSSL_MD_SHA256
167#define SIG_RSA_SHA384 POLARSSL_MD_SHA384
168#define SIG_RSA_SHA512 POLARSSL_MD_SHA512
169#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
170#warning "rsa_pkcs1_verify() prototype changed. Manual change required if used"
171#warning "rsa_pkcs1_decrypt() prototype changed. Manual change required if used"
172#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200173#endif /* POLARSSL_RSA_C */
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200174
175#if defined(POLARSSL_DHM_C)
176#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
177#warning "dhm_calc_secret() prototype changed. Manual change required if used"
178#endif
179#endif
180
181#if defined(POLARSSL_GCM_C)
182#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
183#warning "gcm_init() prototype changed. Manual change required if used"
184#endif
185#endif
186
187#if defined(POLARSSL_SSL_CLI_C)
188#if defined(SHOW_PROTOTYPE_CHANGE_WARNINGS)
189#warning "ssl_set_own_cert() prototype changed. Change to ssl_set_own_cert_rsa(). Manual change required if used"
190#endif
191#endif
192
Paul Bakker51876562013-09-17 14:36:05 +0200193#if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
Paul Bakker86d0c192013-09-18 11:11:02 +0200194#include "x509.h"
195
Paul Bakker51876562013-09-17 14:36:05 +0200196#define POLARSSL_ERR_X509_CERT_INVALID_FORMAT POLARSSL_ERR_X509_INVALID_FORMAT
197#define POLARSSL_ERR_X509_CERT_INVALID_VERSION POLARSSL_ERR_X509_INVALID_VERSION
198#define POLARSSL_ERR_X509_CERT_INVALID_ALG POLARSSL_ERR_X509_INVALID_ALG
199#define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG POLARSSL_ERR_X509_UNKNOWN_SIG_ALG
200#define POLARSSL_ERR_X509_CERT_INVALID_NAME POLARSSL_ERR_X509_INVALID_NAME
201#define POLARSSL_ERR_X509_CERT_INVALID_DATE POLARSSL_ERR_X509_INVALID_DATE
202#define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS POLARSSL_ERR_X509_INVALID_EXTENSIONS
203#define POLARSSL_ERR_X509_CERT_SIG_MISMATCH POLARSSL_ERR_X509_SIG_MISMATCH
204#define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE POLARSSL_ERR_X509_INVALID_SIGNATURE
205#define POLARSSL_ERR_X509_CERT_INVALID_SERIAL POLARSSL_ERR_X509_INVALID_SERIAL
206#define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION POLARSSL_ERR_X509_UNKNOWN_VERSION
Paul Bakker86d0c192013-09-18 11:11:02 +0200207
Steffan Karger44cf68f2013-11-12 10:34:55 +0100208static inline int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200209 return x509_serial_gets( buf, size, serial );
210}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100211static inline int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200212 return x509_dn_gets( buf, size, dn );
213}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100214static inline int x509parse_time_expired( const x509_time *time ) {
Paul Bakker86d0c192013-09-18 11:11:02 +0200215 return x509_time_expired( time );
216}
Paul Bakker51876562013-09-17 14:36:05 +0200217#endif /* POLARSSL_X509_USE_C || POLARSSL_X509_CREATE_C */
218
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200219#if defined(POLARSSL_X509_CRT_PARSE_C)
220#define POLARSSL_X509_PARSE_C
221#include "x509_crt.h"
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200222typedef x509_crt x509_cert;
223
Steffan Karger44cf68f2013-11-12 10:34:55 +0100224static inline int x509parse_crt_der( x509_cert *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200225 size_t buflen ) {
226 return x509_crt_parse_der( chain, buf, buflen );
227}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100228static inline int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200229 return x509_crt_parse( chain, buf, buflen );
230}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100231static inline int x509parse_crtfile( x509_cert *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200232 return x509_crt_parse_file( chain, path );
233}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100234static inline int x509parse_crtpath( x509_cert *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200235 return x509_crt_parse_path( chain, path );
236}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100237static inline int x509parse_cert_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200238 const x509_cert *crt ) {
239 return x509_crt_info( buf, size, prefix, crt );
240}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100241static inline int x509parse_verify( x509_cert *crt, x509_cert *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200242 x509_crl *ca_crl, const char *cn, int *flags,
243 int (*f_vrfy)(void *, x509_cert *, int, int *),
244 void *p_vrfy ) {
245 return x509_crt_verify( crt, trust_ca, ca_crl, cn, flags, f_vrfy, p_vrfy );
246}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100247static inline int x509parse_revoked( const x509_cert *crt, const x509_crl *crl ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200248 return x509_crt_revoked( crt, crl );
249}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100250static inline void x509_free( x509_cert *crt ) {
Paul Bakker4aa40d42013-10-11 10:49:24 +0200251 x509_crt_free( crt );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200252}
253#endif /* POLARSSL_X509_CRT_PARSE_C */
254
Paul Bakkerddf26b42013-09-18 13:46:23 +0200255#if defined(POLARSSL_X509_CRL_PARSE_C)
256#define POLARSSL_X509_PARSE_C
257#include "x509_crl.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100258static inline int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200259 return x509_crl_parse( chain, buf, buflen );
260}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100261static inline int x509parse_crlfile( x509_crl *chain, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200262 return x509_crl_parse_file( chain, path );
263}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100264static inline int x509parse_crl_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200265 const x509_crl *crl ) {
266 return x509_crl_info( buf, size, prefix, crl );
267}
268#endif /* POLARSSL_X509_CRL_PARSE_C */
269
270#if defined(POLARSSL_X509_CSR_PARSE_C)
271#define POLARSSL_X509_PARSE_C
272#include "x509_csr.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100273static inline int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200274 return x509_csr_parse( csr, buf, buflen );
275}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100276static inline int x509parse_csrfile( x509_csr *csr, const char *path ) {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200277 return x509_csr_parse_file( csr, path );
278}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100279static inline int x509parse_csr_info( char *buf, size_t size, const char *prefix,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200280 const x509_csr *csr ) {
281 return x509_csr_info( buf, size, prefix, csr );
282}
283#endif /* POLARSSL_X509_CSR_PARSE_C */
284
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200285#if defined(POLARSSL_SSL_TLS_C)
286#include "ssl_ciphersuites.h"
287
288#define ssl_default_ciphersuites ssl_list_ciphersuites()
289#endif
290
291#if defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_RSA_C)
292#include "rsa.h"
293#include "pk.h"
294
295#define POLARSSL_ERR_X509_PASSWORD_MISMATCH POLARSSL_ERR_PK_PASSWORD_MISMATCH
296#define POLARSSL_ERR_X509_KEY_INVALID_FORMAT POLARSSL_ERR_PK_KEY_INVALID_FORMAT
297#define POLARSSL_ERR_X509_UNKNOWN_PK_ALG POLARSSL_ERR_PK_UNKNOWN_PK_ALG
298#define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY POLARSSL_ERR_PK_INVALID_PUBKEY
299
300#if defined(POLARSSL_FS_IO)
Steffan Karger44cf68f2013-11-12 10:34:55 +0100301static inline int x509parse_keyfile( rsa_context *rsa, const char *path,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200302 const char *pwd ) {
303 int ret;
304 pk_context pk;
305 pk_init( &pk );
306 ret = pk_parse_keyfile( &pk, path, pwd );
307 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
308 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
309 if( ret == 0 )
310 rsa_copy( rsa, pk_rsa( pk ) );
311 else
312 rsa_free( rsa );
313 pk_free( &pk );
314 return( ret );
315}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100316static inline int x509parse_public_keyfile( rsa_context *rsa, const char *path ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200317 int ret;
318 pk_context pk;
319 pk_init( &pk );
320 ret = pk_parse_public_keyfile( &pk, path );
321 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
322 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
323 if( ret == 0 )
324 rsa_copy( rsa, pk_rsa( pk ) );
325 else
326 rsa_free( rsa );
327 pk_free( &pk );
328 return( ret );
329}
330#endif /* POLARSSL_FS_IO */
331
Steffan Karger44cf68f2013-11-12 10:34:55 +0100332static inline int x509parse_key( rsa_context *rsa, const unsigned char *key,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200333 size_t keylen,
334 const unsigned char *pwd, size_t pwdlen ) {
335 int ret;
336 pk_context pk;
337 pk_init( &pk );
338 ret = pk_parse_key( &pk, key, keylen, pwd, pwdlen );
339 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
340 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
341 if( ret == 0 )
342 rsa_copy( rsa, pk_rsa( pk ) );
343 else
344 rsa_free( rsa );
345 pk_free( &pk );
346 return( ret );
347}
348
Steffan Karger44cf68f2013-11-12 10:34:55 +0100349static inline int x509parse_public_key( rsa_context *rsa,
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200350 const unsigned char *key, size_t keylen )
351{
352 int ret;
353 pk_context pk;
354 pk_init( &pk );
355 ret = pk_parse_public_key( &pk, key, keylen );
356 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
357 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
358 if( ret == 0 )
359 rsa_copy( rsa, pk_rsa( pk ) );
360 else
361 rsa_free( rsa );
362 pk_free( &pk );
363 return( ret );
364}
365#endif /* POLARSSL_PK_PARSE_C && POLARSSL_RSA_C */
366
367#if defined(POLARSSL_PK_WRITE_C) && defined(POLARSSL_RSA_C)
368#include "pk.h"
Steffan Karger44cf68f2013-11-12 10:34:55 +0100369static inline int x509_write_pubkey_der( unsigned char *buf, size_t len, rsa_context *rsa ) {
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200370 int ret;
371 pk_context ctx;
372 if( ( ret = pk_init_ctx( &ctx, pk_info_from_type( POLARSSL_PK_RSA ) ) ) != 0 ) return( ret );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200373 if( ( ret = rsa_copy( pk_rsa( ctx ), rsa ) ) != 0 ) return( ret );
Paul Bakkerd1a983f2013-09-16 22:26:53 +0200374 ret = pk_write_pubkey_der( &ctx, buf, len );
375 pk_free( &ctx );
376 return( ret );
377}
Steffan Karger44cf68f2013-11-12 10:34:55 +0100378static inline int x509_write_key_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_key_der( &ctx, buf, len );
384 pk_free( &ctx );
385 return( ret );
386}
387#endif /* POLARSSL_PK_WRITE_C && POLARSSL_RSA_C */
388#endif /* compat-1.2.h */