proxmark3/common/mbedtls/pkwrite.c

496 lines
16 KiB
C
Raw Normal View History

2018-11-14 01:02:02 +08:00
/*
* Public Key layer for writing key files and structures
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: GPL-2.0
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_PK_WRITE_C)
#include "mbedtls/pk.h"
#include "mbedtls/asn1write.h"
#include "mbedtls/oid.h"
#include <string.h>
#if defined(MBEDTLS_RSA_C)
#include "mbedtls/rsa.h"
#endif
#if defined(MBEDTLS_ECP_C)
#include "mbedtls/ecp.h"
#endif
#if defined(MBEDTLS_ECDSA_C)
#include "mbedtls/ecdsa.h"
#endif
#if defined(MBEDTLS_PEM_WRITE_C)
#include "mbedtls/pem.h"
#endif
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
#include <stdlib.h>
#define mbedtls_calloc calloc
#define mbedtls_free free
#endif
#if defined(MBEDTLS_RSA_C)
/*
* RSAPublicKey ::= SEQUENCE {
* modulus INTEGER, -- n
* publicExponent INTEGER -- e
* }
*/
2019-03-10 07:00:59 +08:00
static int pk_write_rsa_pubkey(unsigned char **p, unsigned char *start,
mbedtls_rsa_context *rsa) {
2018-11-14 01:02:02 +08:00
int ret;
size_t len = 0;
mbedtls_mpi T;
2019-03-10 07:00:59 +08:00
mbedtls_mpi_init(&T);
2018-11-14 01:02:02 +08:00
/* Export E */
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
(ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0)
2018-11-14 01:02:02 +08:00
goto end_of_export;
len += ret;
/* Export N */
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
(ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0)
2018-11-14 01:02:02 +08:00
goto end_of_export;
len += ret;
end_of_export:
2019-03-10 07:00:59 +08:00
mbedtls_mpi_free(&T);
if (ret < 0)
return (ret);
2018-11-14 01:02:02 +08:00
2019-03-10 07:00:59 +08:00
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
MBEDTLS_ASN1_SEQUENCE));
2018-11-14 01:02:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-14 01:02:02 +08:00
}
#endif /* MBEDTLS_RSA_C */
#if defined(MBEDTLS_ECP_C)
/*
* EC public key is an EC point
*/
2019-03-10 07:00:59 +08:00
static int pk_write_ec_pubkey(unsigned char **p, unsigned char *start,
mbedtls_ecp_keypair *ec) {
2018-11-14 01:02:02 +08:00
int ret;
size_t len = 0;
unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_ecp_point_write_binary(&ec->grp, &ec->Q,
MBEDTLS_ECP_PF_UNCOMPRESSED,
&len, buf, sizeof(buf))) != 0) {
return (ret);
2018-11-14 01:02:02 +08:00
}
2019-03-10 07:00:59 +08:00
if (*p < start || (size_t)(*p - start) < len)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-14 01:02:02 +08:00
*p -= len;
2019-03-10 07:00:59 +08:00
memcpy(*p, buf, len);
2018-11-14 01:02:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-14 01:02:02 +08:00
}
/*
* ECParameters ::= CHOICE {
* namedCurve OBJECT IDENTIFIER
* }
*/
2019-03-10 07:00:59 +08:00
static int pk_write_ec_param(unsigned char **p, unsigned char *start,
mbedtls_ecp_keypair *ec) {
2018-11-14 01:02:02 +08:00
int ret;
size_t len = 0;
const char *oid;
size_t oid_len;
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_oid_get_oid_by_ec_grp(ec->grp.id, &oid, &oid_len)) != 0)
return (ret);
2018-11-14 01:02:02 +08:00
2019-03-10 07:00:59 +08:00
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
2018-11-14 01:02:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-14 01:02:02 +08:00
}
#endif /* MBEDTLS_ECP_C */
2019-03-10 07:00:59 +08:00
int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
const mbedtls_pk_context *key) {
2018-11-14 01:02:02 +08:00
int ret;
size_t len = 0;
#if defined(MBEDTLS_RSA_C)
2019-03-10 07:00:59 +08:00
if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA)
MBEDTLS_ASN1_CHK_ADD(len, pk_write_rsa_pubkey(p, start, mbedtls_pk_rsa(*key)));
2018-11-14 01:02:02 +08:00
else
#endif
#if defined(MBEDTLS_ECP_C)
2019-03-10 07:00:59 +08:00
if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY)
MBEDTLS_ASN1_CHK_ADD(len, pk_write_ec_pubkey(p, start, mbedtls_pk_ec(*key)));
else
2018-11-14 01:02:02 +08:00
#endif
2019-03-10 07:00:59 +08:00
return (MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE);
2018-11-14 01:02:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-14 01:02:02 +08:00
}
int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *key, unsigned char *buf, size_t size) {
2018-11-14 01:02:02 +08:00
int ret;
unsigned char *c;
size_t len = 0, par_len = 0, oid_len;
const char *oid;
c = buf + size;
2019-03-10 07:00:59 +08:00
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_pk_write_pubkey(&c, buf, key));
2018-11-14 01:02:02 +08:00
2019-03-10 07:00:59 +08:00
if (c - buf < 1)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-14 01:02:02 +08:00
/*
* SubjectPublicKeyInfo ::= SEQUENCE {
* algorithm AlgorithmIdentifier,
* subjectPublicKey BIT STRING }
*/
*--c = 0;
len += 1;
2019-03-10 07:00:59 +08:00
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING));
2018-11-14 01:02:02 +08:00
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_oid_get_oid_by_pk_alg(mbedtls_pk_get_type(key),
&oid, &oid_len)) != 0) {
return (ret);
2018-11-14 01:02:02 +08:00
}
#if defined(MBEDTLS_ECP_C)
2019-03-10 07:00:59 +08:00
if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, mbedtls_pk_ec(*key)));
2018-11-14 01:02:02 +08:00
}
#endif
2019-03-10 07:00:59 +08:00
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_algorithm_identifier(&c, buf, oid, oid_len,
par_len));
2018-11-14 01:02:02 +08:00
2019-03-10 07:00:59 +08:00
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
MBEDTLS_ASN1_SEQUENCE));
2018-11-14 01:02:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-14 01:02:02 +08:00
}
int mbedtls_pk_write_key_der(mbedtls_pk_context *key, unsigned char *buf, size_t size) {
2018-11-14 01:02:02 +08:00
int ret;
unsigned char *c = buf + size;
size_t len = 0;
#if defined(MBEDTLS_RSA_C)
2019-03-10 07:00:59 +08:00
if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
2018-11-14 01:02:02 +08:00
mbedtls_mpi T; /* Temporary holding the exported parameters */
2019-03-10 07:00:59 +08:00
mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*key);
2018-11-14 01:02:02 +08:00
/*
* Export the parameters one after another to avoid simultaneous copies.
*/
2019-03-10 07:00:59 +08:00
mbedtls_mpi_init(&T);
2018-11-14 01:02:02 +08:00
/* Export QP */
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
2018-11-14 01:02:02 +08:00
goto end_of_export;
len += ret;
/* Export DQ */
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
2018-11-14 01:02:02 +08:00
goto end_of_export;
len += ret;
/* Export DP */
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
2018-11-14 01:02:02 +08:00
goto end_of_export;
len += ret;
/* Export Q */
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
&T, NULL, NULL)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
2018-11-14 01:02:02 +08:00
goto end_of_export;
len += ret;
/* Export P */
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_rsa_export(rsa, NULL, &T,
NULL, NULL, NULL)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
2018-11-14 01:02:02 +08:00
goto end_of_export;
len += ret;
/* Export D */
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
NULL, &T, NULL)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
2018-11-14 01:02:02 +08:00
goto end_of_export;
len += ret;
/* Export E */
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
NULL, NULL, &T)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
2018-11-14 01:02:02 +08:00
goto end_of_export;
len += ret;
/* Export N */
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_rsa_export(rsa, &T, NULL,
NULL, NULL, NULL)) != 0 ||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
2018-11-14 01:02:02 +08:00
goto end_of_export;
len += ret;
2019-03-10 07:00:59 +08:00
end_of_export:
2018-11-14 01:02:02 +08:00
2019-03-10 07:00:59 +08:00
mbedtls_mpi_free(&T);
if (ret < 0)
return (ret);
2018-11-14 01:02:02 +08:00
2019-03-10 07:00:59 +08:00
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c,
buf, MBEDTLS_ASN1_CONSTRUCTED |
MBEDTLS_ASN1_SEQUENCE));
} else
2018-11-14 01:02:02 +08:00
#endif /* MBEDTLS_RSA_C */
#if defined(MBEDTLS_ECP_C)
2019-03-10 07:00:59 +08:00
if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*key);
size_t pub_len = 0, par_len = 0;
/*
* RFC 5915, or SEC1 Appendix C.4
*
* ECPrivateKey ::= SEQUENCE {
* version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
* privateKey OCTET STRING,
* parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
* publicKey [1] BIT STRING OPTIONAL
* }
*/
/* publicKey */
MBEDTLS_ASN1_CHK_ADD(pub_len, pk_write_ec_pubkey(&c, buf, ec));
if (c - buf < 1)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
*--c = 0;
pub_len += 1;
MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len));
MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_BIT_STRING));
MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_len(&c, buf, pub_len));
MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_asn1_write_tag(&c, buf,
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1));
len += pub_len;
/* parameters */
MBEDTLS_ASN1_CHK_ADD(par_len, pk_write_ec_param(&c, buf, ec));
MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_len(&c, buf, par_len));
MBEDTLS_ASN1_CHK_ADD(par_len, mbedtls_asn1_write_tag(&c, buf,
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0));
len += par_len;
/* privateKey: write as MPI then fix tag */
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&c, buf, &ec->d));
*c = MBEDTLS_ASN1_OCTET_STRING;
/* version */
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 1));
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
MBEDTLS_ASN1_SEQUENCE));
} else
2018-11-14 01:02:02 +08:00
#endif /* MBEDTLS_ECP_C */
2019-03-10 07:00:59 +08:00
return (MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE);
2018-11-14 01:02:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-14 01:02:02 +08:00
}
#if defined(MBEDTLS_PEM_WRITE_C)
#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
/*
* Max sizes of key per types. Shown as tag + len (+ content).
*/
#if defined(MBEDTLS_RSA_C)
/*
* RSA public keys:
* SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
* algorithm AlgorithmIdentifier, 1 + 1 (sequence)
* + 1 + 1 + 9 (rsa oid)
* + 1 + 1 (params null)
* subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
* RSAPublicKey ::= SEQUENCE { 1 + 3
* modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
* publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
* }
*/
#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
/*
* RSA private keys:
* RSAPrivateKey ::= SEQUENCE { 1 + 3
* version Version, 1 + 1 + 1
* modulus INTEGER, 1 + 3 + MPI_MAX + 1
* publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
* privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
* prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
* prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
* exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
* exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
* coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
* otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
* }
*/
#define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
2019-03-10 07:00:59 +08:00
MBEDTLS_MPI_MAX_SIZE % 2
2018-11-14 01:02:02 +08:00
#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
2019-03-10 07:00:59 +08:00
+ 5 * MPI_MAX_SIZE_2
2018-11-14 01:02:02 +08:00
#else /* MBEDTLS_RSA_C */
#define RSA_PUB_DER_MAX_BYTES 0
#define RSA_PRV_DER_MAX_BYTES 0
#endif /* MBEDTLS_RSA_C */
#if defined(MBEDTLS_ECP_C)
/*
* EC public keys:
* SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
* algorithm AlgorithmIdentifier, 1 + 1 (sequence)
* + 1 + 1 + 7 (ec oid)
* + 1 + 1 + 9 (namedCurve oid)
* subjectPublicKey BIT STRING 1 + 2 + 1 [1]
* + 1 (point format) [1]
* + 2 * ECP_MAX (coords) [1]
* }
*/
#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
/*
* EC private keys:
* ECPrivateKey ::= SEQUENCE { 1 + 2
* version INTEGER , 1 + 1 + 1
* privateKey OCTET STRING, 1 + 1 + ECP_MAX
* parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
* publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
* }
*/
#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
#else /* MBEDTLS_ECP_C */
#define ECP_PUB_DER_MAX_BYTES 0
#define ECP_PRV_DER_MAX_BYTES 0
#endif /* MBEDTLS_ECP_C */
#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
2019-03-10 07:00:59 +08:00
RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
2018-11-14 01:02:02 +08:00
#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
2019-03-10 07:00:59 +08:00
RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
2018-11-14 01:02:02 +08:00
int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *key, unsigned char *buf, size_t size) {
2018-11-14 01:02:02 +08:00
int ret;
unsigned char output_buf[PUB_DER_MAX_BYTES];
size_t olen = 0;
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_pk_write_pubkey_der(key, output_buf,
sizeof(output_buf))) < 0) {
return (ret);
2018-11-14 01:02:02 +08:00
}
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
output_buf + sizeof(output_buf) - ret,
ret, buf, size, &olen)) != 0) {
return (ret);
2018-11-14 01:02:02 +08:00
}
2019-03-10 07:00:59 +08:00
return (0);
2018-11-14 01:02:02 +08:00
}
int mbedtls_pk_write_key_pem(mbedtls_pk_context *key, unsigned char *buf, size_t size) {
2018-11-14 01:02:02 +08:00
int ret;
unsigned char output_buf[PRV_DER_MAX_BYTES];
const char *begin, *end;
size_t olen = 0;
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_pk_write_key_der(key, output_buf, sizeof(output_buf))) < 0)
return (ret);
2018-11-14 01:02:02 +08:00
#if defined(MBEDTLS_RSA_C)
2019-03-10 07:00:59 +08:00
if (mbedtls_pk_get_type(key) == MBEDTLS_PK_RSA) {
2018-11-14 01:02:02 +08:00
begin = PEM_BEGIN_PRIVATE_KEY_RSA;
end = PEM_END_PRIVATE_KEY_RSA;
2019-03-10 07:00:59 +08:00
} else
2018-11-14 01:02:02 +08:00
#endif
#if defined(MBEDTLS_ECP_C)
2019-03-10 07:00:59 +08:00
if (mbedtls_pk_get_type(key) == MBEDTLS_PK_ECKEY) {
begin = PEM_BEGIN_PRIVATE_KEY_EC;
end = PEM_END_PRIVATE_KEY_EC;
} else
2018-11-14 01:02:02 +08:00
#endif
2019-03-10 07:00:59 +08:00
return (MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE);
2018-11-14 01:02:02 +08:00
2019-03-10 07:00:59 +08:00
if ((ret = mbedtls_pem_write_buffer(begin, end,
output_buf + sizeof(output_buf) - ret,
ret, buf, size, &olen)) != 0) {
return (ret);
2018-11-14 01:02:02 +08:00
}
2019-03-10 07:00:59 +08:00
return (0);
2018-11-14 01:02:02 +08:00
}
#endif /* MBEDTLS_PEM_WRITE_C */
#endif /* MBEDTLS_PK_WRITE_C */