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 )
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.