47#if !defined(MBEDTLS_CONFIG_FILE)
50#include MBEDTLS_CONFIG_FILE
53#if defined(MBEDTLS_PK_WRITE_C)
62#if defined(MBEDTLS_RSA_C)
65#if defined(MBEDTLS_ECP_C)
70#if defined(MBEDTLS_ECDSA_C)
73#if defined(MBEDTLS_PEM_WRITE_C)
77#if defined(MBEDTLS_PLATFORM_C)
81#define mbedtls_calloc calloc
82#define mbedtls_free free
86#define PK_VALIDATE_RET( cond ) \
87 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
88#define PK_VALIDATE( cond ) \
89 MBEDTLS_INTERNAL_VALIDATE( cond )
91#if defined(MBEDTLS_RSA_C)
98static int pk_write_rsa_pubkey(
unsigned char **
p,
unsigned char *
start,
133#if defined(MBEDTLS_ECP_C)
137static int pk_write_ec_pubkey(
unsigned char **
p,
unsigned char *
start,
165static int pk_write_ec_param(
unsigned char **
p,
unsigned char *
start,
184static int pk_write_ec_private(
unsigned char **
p,
unsigned char *
start,
188 size_t byte_length = ( ec->
grp.
pbits + 7 ) / 8;
202int mbedtls_pk_write_pubkey(
unsigned char **
p,
unsigned char *
start,
208 PK_VALIDATE_RET(
p !=
NULL );
209 PK_VALIDATE_RET( *
p !=
NULL );
211 PK_VALIDATE_RET(
key !=
NULL );
213#if defined(MBEDTLS_RSA_C)
218#if defined(MBEDTLS_ECP_C)
232 size_t len = 0, par_len = 0, oid_len;
235 PK_VALIDATE_RET(
key !=
NULL );
238 PK_VALIDATE_RET(
buf !=
NULL );
259 &oid, &oid_len ) ) != 0 )
264#if defined(MBEDTLS_ECP_C)
287 PK_VALIDATE_RET(
key !=
NULL );
290 PK_VALIDATE_RET(
buf !=
NULL );
294#if defined(MBEDTLS_RSA_C)
373#if defined(MBEDTLS_ECP_C)
377 size_t pub_len = 0, par_len = 0;
431#if defined(MBEDTLS_PEM_WRITE_C)
433#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
434#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
436#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
437#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
438#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
439#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
445#if defined(MBEDTLS_RSA_C)
458#define RSA_PUB_DER_MAX_BYTES ( 38 + 2 * MBEDTLS_MPI_MAX_SIZE )
475#define MPI_MAX_SIZE_2 ( MBEDTLS_MPI_MAX_SIZE / 2 + \
476 MBEDTLS_MPI_MAX_SIZE % 2 )
477#define RSA_PRV_DER_MAX_BYTES ( 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
478 + 5 * MPI_MAX_SIZE_2 )
482#define RSA_PUB_DER_MAX_BYTES 0
483#define RSA_PRV_DER_MAX_BYTES 0
487#if defined(MBEDTLS_ECP_C)
499#define ECP_PUB_DER_MAX_BYTES ( 30 + 2 * MBEDTLS_ECP_MAX_BYTES )
510#define ECP_PRV_DER_MAX_BYTES ( 29 + 3 * MBEDTLS_ECP_MAX_BYTES )
514#define ECP_PUB_DER_MAX_BYTES 0
515#define ECP_PRV_DER_MAX_BYTES 0
519#define PUB_DER_MAX_BYTES ( RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
520 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES )
521#define PRV_DER_MAX_BYTES ( RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
522 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES )
530 PK_VALIDATE_RET(
key !=
NULL );
539 if( (
ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
556 PK_VALIDATE_RET(
key !=
NULL );
562#if defined(MBEDTLS_RSA_C)
565 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
566 end = PEM_END_PRIVATE_KEY_RSA;
570#if defined(MBEDTLS_ECP_C)
573 begin = PEM_BEGIN_PRIVATE_KEY_EC;
574 end = PEM_END_PRIVATE_KEY_EC;
ASN.1 buffer writing functionality.
int mbedtls_asn1_write_tag(unsigned char **p, unsigned char *start, unsigned char tag)
Write an ASN.1 tag in ASN.1 format.
#define MBEDTLS_ASN1_CHK_ADD(g, f)
int mbedtls_asn1_write_octet_string(unsigned char **p, unsigned char *start, const unsigned char *buf, size_t size)
Write an octet string tag (MBEDTLS_ASN1_OCTET_STRING) and value in ASN.1 format.
int mbedtls_asn1_write_int(unsigned char **p, unsigned char *start, int val)
Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format.
int mbedtls_asn1_write_len(unsigned char **p, unsigned char *start, size_t len)
Write a length field in ASN.1 format.
int mbedtls_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.
int mbedtls_asn1_write_oid(unsigned char **p, unsigned char *start, const char *oid, size_t oid_len)
Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format.
int mbedtls_asn1_write_mpi(unsigned char **p, unsigned char *start, const mbedtls_mpi *X)
Write a arbitrary-precision number (MBEDTLS_ASN1_INTEGER) in ASN.1 format.
Multi-precision integer library.
void mbedtls_mpi_init(mbedtls_mpi *X)
Initialize an MPI context.
void mbedtls_mpi_free(mbedtls_mpi *X)
This function frees the components of an MPI context.
int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
Export an MPI into unsigned big endian binary data of fixed size.
This file contains ECDSA definitions and functions.
GLenum GLuint GLenum GLsizei const GLchar * buf
#define MBEDTLS_ASN1_SEQUENCE
#define MBEDTLS_ASN1_CONTEXT_SPECIFIC
#define MBEDTLS_ASN1_CONSTRUCTED
#define MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
#define MBEDTLS_ASN1_BIT_STRING
jpeg_component_info JCOEFPTR JSAMPARRAY output_buf
#define memcpy(s1, s2, n)
Object Identifier (OID) database.
int mbedtls_oid_get_oid_by_ec_grp(mbedtls_ecp_group_id grp_id, const char **oid, size_t *olen)
Translate EC group identifier into NamedCurve OID.
int mbedtls_oid_get_oid_by_pk_alg(mbedtls_pk_type_t pk_alg, const char **oid, size_t *olen)
Translate pk_type into PublicKeyAlgorithm OID.
Privacy Enhanced Mail (PEM) decoding.
Public Key abstraction layer.
static mbedtls_rsa_context * mbedtls_pk_rsa(const mbedtls_pk_context pk)
static mbedtls_ecp_keypair * mbedtls_pk_ec(const mbedtls_pk_context pk)
mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Get the key type.
#define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE
This file provides an API for the RSA public-key cryptosystem.
int mbedtls_rsa_export(const mbedtls_rsa_context *ctx, mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q, mbedtls_mpi *D, mbedtls_mpi *E)
This function exports the core parameters of an RSA key.
int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx, mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
This function exports CRT parameters of a private RSA key.
Configuration options (set of defines)
This file provides an API for Elliptic Curves over GF(P) (ECP).
#define MBEDTLS_ECP_PF_UNCOMPRESSED
#define MBEDTLS_ECP_MAX_PT_LEN
#define MBEDTLS_ECP_MAX_BYTES
int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P, int format, size_t *olen, unsigned char *buf, size_t buflen)
This function exports a point into unsigned binary data.
The ECP key-pair structure.
The RSA context structure.