28 #if defined(POLARSSL_PK_WRITE_C)
34 #if defined(POLARSSL_RSA_C)
37 #if defined(POLARSSL_ECP_C)
40 #if defined(POLARSSL_ECDSA_C)
43 #if defined(POLARSSL_PEM_WRITE_C)
47 #if defined(POLARSSL_MEMORY_C)
51 #define polarssl_malloc malloc
52 #define polarssl_free free
55 #if defined(POLARSSL_RSA_C)
62 static int pk_write_rsa_pubkey(
unsigned char **p,
unsigned char *start,
78 #if defined(POLARSSL_ECP_C)
82 static int pk_write_ec_pubkey(
unsigned char **p,
unsigned char *start,
91 &len, buf,
sizeof( buf ) ) ) != 0 )
96 if( *p - start < (
int) len )
100 memcpy( *p, buf, len );
110 static int pk_write_ec_param(
unsigned char **p,
unsigned char *start,
133 #if defined(POLARSSL_RSA_C)
138 #if defined(POLARSSL_ECP_C)
152 size_t len = 0, par_len = 0, oid_len;
174 &oid, &oid_len ) ) != 0 )
179 #if defined(POLARSSL_ECP_C)
198 unsigned char *c = buf + size;
201 #if defined(POLARSSL_RSA_C)
221 #if defined(POLARSSL_ECP_C)
225 size_t pub_len = 0, par_len = 0;
239 ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
255 ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
279 #if defined(POLARSSL_PEM_WRITE_C)
281 #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
282 #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
284 #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
285 #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
286 #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
287 #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
292 unsigned char output_buf[4096];
296 sizeof(output_buf) ) ) < 0 )
301 if( ( ret = pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
302 output_buf +
sizeof(output_buf) - ret,
303 ret, buf, size, &olen ) ) != 0 )
314 unsigned char output_buf[4096];
315 const char *begin, *end;
321 #if defined(POLARSSL_RSA_C)
324 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
325 end = PEM_END_PRIVATE_KEY_RSA;
329 #if defined(POLARSSL_ECP_C)
332 begin = PEM_BEGIN_PRIVATE_KEY_EC;
333 end = PEM_END_PRIVATE_KEY_EC;
339 if( ( ret = pem_write_buffer( begin, end,
340 output_buf +
sizeof(output_buf) - ret,
341 ret, buf, size, &olen ) ) != 0 )