26 #if !defined(POLARSSL_CONFIG_FILE)
29 #include POLARSSL_CONFIG_FILE
32 #if defined(POLARSSL_PK_WRITE_C)
38 #if defined(POLARSSL_RSA_C)
41 #if defined(POLARSSL_ECP_C)
44 #if defined(POLARSSL_ECDSA_C)
47 #if defined(POLARSSL_PEM_WRITE_C)
51 #if defined(POLARSSL_PLATFORM_C)
55 #define polarssl_malloc malloc
56 #define polarssl_free free
59 #if defined(POLARSSL_RSA_C)
66 static int pk_write_rsa_pubkey(
unsigned char **p,
unsigned char *start,
83 #if defined(POLARSSL_ECP_C)
87 static int pk_write_ec_pubkey(
unsigned char **p,
unsigned char *start,
96 &len, buf,
sizeof( buf ) ) ) != 0 )
101 if( *p - start < (
int) len )
105 memcpy( *p, buf, len );
115 static int pk_write_ec_param(
unsigned char **p,
unsigned char *start,
138 #if defined(POLARSSL_RSA_C)
143 #if defined(POLARSSL_ECP_C)
157 size_t len = 0, par_len = 0, oid_len;
179 &oid, &oid_len ) ) != 0 )
184 #if defined(POLARSSL_ECP_C)
204 unsigned char *c = buf + size;
207 #if defined(POLARSSL_RSA_C)
228 #if defined(POLARSSL_ECP_C)
232 size_t pub_len = 0, par_len = 0;
246 ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
262 ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
287 #if defined(POLARSSL_PEM_WRITE_C)
289 #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
290 #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
292 #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
293 #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
294 #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
295 #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
301 #if defined(POLARSSL_RSA_C)
314 #define RSA_PUB_DER_MAX_BYTES 38 + 2 * POLARSSL_MPI_MAX_SIZE
331 #define MPI_MAX_SIZE_2 POLARSSL_MPI_MAX_SIZE / 2 + \
332 POLARSSL_MPI_MAX_SIZE % 2
333 #define RSA_PRV_DER_MAX_BYTES 47 + 3 * POLARSSL_MPI_MAX_SIZE \
338 #define RSA_PUB_DER_MAX_BYTES 0
339 #define RSA_PRV_DER_MAX_BYTES 0
343 #if defined(POLARSSL_ECP_C)
355 #define ECP_PUB_DER_MAX_BYTES 30 + 2 * POLARSSL_ECP_MAX_BYTES
366 #define ECP_PRV_DER_MAX_BYTES 29 + 3 * POLARSSL_ECP_MAX_BYTES
370 #define ECP_PUB_DER_MAX_BYTES 0
371 #define ECP_PRV_DER_MAX_BYTES 0
375 #define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
376 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
377 #define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
378 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
383 unsigned char output_buf[PUB_DER_MAX_BYTES];
387 sizeof(output_buf) ) ) < 0 )
392 if( ( ret = pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
393 output_buf +
sizeof(output_buf) - ret,
394 ret, buf, size, &olen ) ) != 0 )
405 unsigned char output_buf[PRV_DER_MAX_BYTES];
406 const char *begin, *end;
412 #if defined(POLARSSL_RSA_C)
415 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
416 end = PEM_END_PRIVATE_KEY_RSA;
420 #if defined(POLARSSL_ECP_C)
423 begin = PEM_BEGIN_PRIVATE_KEY_EC;
424 end = PEM_END_PRIVATE_KEY_EC;
430 if( ( ret = pem_write_buffer( begin, end,
431 output_buf +
sizeof(output_buf) - ret,
432 ret, buf, size, &olen ) ) != 0 )