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"
300 unsigned char output_buf[4096];
304 sizeof(output_buf) ) ) < 0 )
309 if( ( ret = pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
310 output_buf +
sizeof(output_buf) - ret,
311 ret, buf, size, &olen ) ) != 0 )
322 unsigned char output_buf[4096];
323 const char *begin, *end;
329 #if defined(POLARSSL_RSA_C)
332 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
333 end = PEM_END_PRIVATE_KEY_RSA;
337 #if defined(POLARSSL_ECP_C)
340 begin = PEM_BEGIN_PRIVATE_KEY_EC;
341 end = PEM_END_PRIVATE_KEY_EC;
347 if( ( ret = pem_write_buffer( begin, end,
348 output_buf +
sizeof(output_buf) - ret,
349 ret, buf, size, &olen ) ) != 0 )
int pk_write_key_der(pk_context *ctx, unsigned char *buf, size_t size)
Write a private key to a PKCS#1 or SEC1 DER structure Note: data is written at the end of the buffer!...
int oid_get_oid_by_ec_grp(ecp_group_id grp_id, const char **oid, size_t *olen)
Translate EC group identifier into NamedCurve OID.
Elliptic curves over GF(p)
int pk_write_key_pem(pk_context *ctx, unsigned char *buf, size_t size)
Write a private key to a PKCS#1 or SEC1 PEM string.
#define POLARSSL_ERR_ASN1_BUF_TOO_SMALL
Buffer too small when writing ASN.1 data structure.
int oid_get_oid_by_pk_alg(pk_type_t pk_alg, const char **oid, size_t *olen)
Translate pk_type into PublicKeyAlgorithm OID.
#define POLARSSL_ECP_PF_UNCOMPRESSED
Uncompressed point format.
Configuration options (set of defines)
#define pk_ec(pk)
Quick access to an EC context inside a PK context.
pk_type_t pk_get_type(const pk_context *ctx)
Get the key type.
Object Identifier (OID) database.
Public Key abstraction layer.
int asn1_write_len(unsigned char **p, unsigned char *start, size_t len)
Write a length field in ASN.1 format Note: function works backwards in data buffer.
#define POLARSSL_ERR_PK_FEATURE_UNAVAILABLE
Unavailable feature, e.g.
int pk_write_pubkey(unsigned char **p, unsigned char *start, const pk_context *key)
Write a subjectPublicKey to ASN.1 data Note: function works backwards in data buffer.
int pk_write_pubkey_der(pk_context *ctx, unsigned char *buf, size_t size)
Write a public key to a SubjectPublicKeyInfo DER structure Note: data is written at the end of the bu...
Privacy Enhanced Mail (PEM) decoding.
#define POLARSSL_ECP_MAX_PT_LEN
#define ASN1_CONTEXT_SPECIFIC
int asn1_write_mpi(unsigned char **p, unsigned char *start, mpi *X)
Write a big number (ASN1_INTEGER) in ASN.1 format Note: function works backwards in data buffer...
int asn1_write_int(unsigned char **p, unsigned char *start, int val)
Write an int tag (ASN1_INTEGER) and value in ASN.1 format Note: function works backwards in data buff...
The RSA public-key cryptosystem.
int ecp_point_write_binary(const ecp_group *grp, const ecp_point *P, int format, size_t *olen, unsigned char *buf, size_t buflen)
Export a point into unsigned binary data.
#define pk_rsa(pk)
Quick access to an RSA context inside a PK context.
#define ASN1_CHK_ADD(g, f)
int asn1_write_algorithm_identifier(unsigned char **p, unsigned char *start, const char *oid, size_t oid_len, size_t par_len)
Write an AlgorithmIdentifier sequence in ASN.1 format Note: function works backwards in data buffer...
ASN.1 buffer writing functionality.
int asn1_write_oid(unsigned char **p, unsigned char *start, const char *oid, size_t oid_len)
Write an OID tag (ASN1_OID) and data in ASN.1 format Note: function works backwards in data buffer...
#define ASN1_OCTET_STRING
int pk_write_pubkey_pem(pk_context *ctx, unsigned char *buf, size_t size)
Write a public key to a PEM string.
int asn1_write_tag(unsigned char **p, unsigned char *start, unsigned char tag)
Write a ASN.1 tag in ASN.1 format Note: function works backwards in data buffer.