Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 /*
marcozecchini 0:9fca2b23d0ba 2 * Public Key layer for writing key files and structures
marcozecchini 0:9fca2b23d0ba 3 *
marcozecchini 0:9fca2b23d0ba 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
marcozecchini 0:9fca2b23d0ba 5 * SPDX-License-Identifier: Apache-2.0
marcozecchini 0:9fca2b23d0ba 6 *
marcozecchini 0:9fca2b23d0ba 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
marcozecchini 0:9fca2b23d0ba 8 * not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 9 * You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 10 *
marcozecchini 0:9fca2b23d0ba 11 * http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 12 *
marcozecchini 0:9fca2b23d0ba 13 * Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
marcozecchini 0:9fca2b23d0ba 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 16 * See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 17 * limitations under the License.
marcozecchini 0:9fca2b23d0ba 18 *
marcozecchini 0:9fca2b23d0ba 19 * This file is part of mbed TLS (https://tls.mbed.org)
marcozecchini 0:9fca2b23d0ba 20 */
marcozecchini 0:9fca2b23d0ba 21
marcozecchini 0:9fca2b23d0ba 22 #if !defined(MBEDTLS_CONFIG_FILE)
marcozecchini 0:9fca2b23d0ba 23 #include "mbedtls/config.h"
marcozecchini 0:9fca2b23d0ba 24 #else
marcozecchini 0:9fca2b23d0ba 25 #include MBEDTLS_CONFIG_FILE
marcozecchini 0:9fca2b23d0ba 26 #endif
marcozecchini 0:9fca2b23d0ba 27
marcozecchini 0:9fca2b23d0ba 28 #if defined(MBEDTLS_PK_WRITE_C)
marcozecchini 0:9fca2b23d0ba 29
marcozecchini 0:9fca2b23d0ba 30 #include "mbedtls/pk.h"
marcozecchini 0:9fca2b23d0ba 31 #include "mbedtls/asn1write.h"
marcozecchini 0:9fca2b23d0ba 32 #include "mbedtls/oid.h"
marcozecchini 0:9fca2b23d0ba 33
marcozecchini 0:9fca2b23d0ba 34 #include <string.h>
marcozecchini 0:9fca2b23d0ba 35
marcozecchini 0:9fca2b23d0ba 36 #if defined(MBEDTLS_RSA_C)
marcozecchini 0:9fca2b23d0ba 37 #include "mbedtls/rsa.h"
marcozecchini 0:9fca2b23d0ba 38 #endif
marcozecchini 0:9fca2b23d0ba 39 #if defined(MBEDTLS_ECP_C)
marcozecchini 0:9fca2b23d0ba 40 #include "mbedtls/ecp.h"
marcozecchini 0:9fca2b23d0ba 41 #endif
marcozecchini 0:9fca2b23d0ba 42 #if defined(MBEDTLS_ECDSA_C)
marcozecchini 0:9fca2b23d0ba 43 #include "mbedtls/ecdsa.h"
marcozecchini 0:9fca2b23d0ba 44 #endif
marcozecchini 0:9fca2b23d0ba 45 #if defined(MBEDTLS_PEM_WRITE_C)
marcozecchini 0:9fca2b23d0ba 46 #include "mbedtls/pem.h"
marcozecchini 0:9fca2b23d0ba 47 #endif
marcozecchini 0:9fca2b23d0ba 48
marcozecchini 0:9fca2b23d0ba 49 #if defined(MBEDTLS_PLATFORM_C)
marcozecchini 0:9fca2b23d0ba 50 #include "mbedtls/platform.h"
marcozecchini 0:9fca2b23d0ba 51 #else
marcozecchini 0:9fca2b23d0ba 52 #include <stdlib.h>
marcozecchini 0:9fca2b23d0ba 53 #define mbedtls_calloc calloc
marcozecchini 0:9fca2b23d0ba 54 #define mbedtls_free free
marcozecchini 0:9fca2b23d0ba 55 #endif
marcozecchini 0:9fca2b23d0ba 56
marcozecchini 0:9fca2b23d0ba 57 #if defined(MBEDTLS_RSA_C)
marcozecchini 0:9fca2b23d0ba 58 /*
marcozecchini 0:9fca2b23d0ba 59 * RSAPublicKey ::= SEQUENCE {
marcozecchini 0:9fca2b23d0ba 60 * modulus INTEGER, -- n
marcozecchini 0:9fca2b23d0ba 61 * publicExponent INTEGER -- e
marcozecchini 0:9fca2b23d0ba 62 * }
marcozecchini 0:9fca2b23d0ba 63 */
marcozecchini 0:9fca2b23d0ba 64 static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
marcozecchini 0:9fca2b23d0ba 65 mbedtls_rsa_context *rsa )
marcozecchini 0:9fca2b23d0ba 66 {
marcozecchini 0:9fca2b23d0ba 67 int ret;
marcozecchini 0:9fca2b23d0ba 68 size_t len = 0;
marcozecchini 0:9fca2b23d0ba 69
marcozecchini 0:9fca2b23d0ba 70 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( p, start, &rsa->E ) );
marcozecchini 0:9fca2b23d0ba 71 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( p, start, &rsa->N ) );
marcozecchini 0:9fca2b23d0ba 72
marcozecchini 0:9fca2b23d0ba 73 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
marcozecchini 0:9fca2b23d0ba 74 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
marcozecchini 0:9fca2b23d0ba 75 MBEDTLS_ASN1_SEQUENCE ) );
marcozecchini 0:9fca2b23d0ba 76
marcozecchini 0:9fca2b23d0ba 77 return( (int) len );
marcozecchini 0:9fca2b23d0ba 78 }
marcozecchini 0:9fca2b23d0ba 79 #endif /* MBEDTLS_RSA_C */
marcozecchini 0:9fca2b23d0ba 80
marcozecchini 0:9fca2b23d0ba 81 #if defined(MBEDTLS_ECP_C)
marcozecchini 0:9fca2b23d0ba 82 /*
marcozecchini 0:9fca2b23d0ba 83 * EC public key is an EC point
marcozecchini 0:9fca2b23d0ba 84 */
marcozecchini 0:9fca2b23d0ba 85 static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
marcozecchini 0:9fca2b23d0ba 86 mbedtls_ecp_keypair *ec )
marcozecchini 0:9fca2b23d0ba 87 {
marcozecchini 0:9fca2b23d0ba 88 int ret;
marcozecchini 0:9fca2b23d0ba 89 size_t len = 0;
marcozecchini 0:9fca2b23d0ba 90 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
marcozecchini 0:9fca2b23d0ba 91
marcozecchini 0:9fca2b23d0ba 92 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
marcozecchini 0:9fca2b23d0ba 93 MBEDTLS_ECP_PF_UNCOMPRESSED,
marcozecchini 0:9fca2b23d0ba 94 &len, buf, sizeof( buf ) ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 95 {
marcozecchini 0:9fca2b23d0ba 96 return( ret );
marcozecchini 0:9fca2b23d0ba 97 }
marcozecchini 0:9fca2b23d0ba 98
marcozecchini 0:9fca2b23d0ba 99 if( *p < start || (size_t)( *p - start ) < len )
marcozecchini 0:9fca2b23d0ba 100 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
marcozecchini 0:9fca2b23d0ba 101
marcozecchini 0:9fca2b23d0ba 102 *p -= len;
marcozecchini 0:9fca2b23d0ba 103 memcpy( *p, buf, len );
marcozecchini 0:9fca2b23d0ba 104
marcozecchini 0:9fca2b23d0ba 105 return( (int) len );
marcozecchini 0:9fca2b23d0ba 106 }
marcozecchini 0:9fca2b23d0ba 107
marcozecchini 0:9fca2b23d0ba 108 /*
marcozecchini 0:9fca2b23d0ba 109 * ECParameters ::= CHOICE {
marcozecchini 0:9fca2b23d0ba 110 * namedCurve OBJECT IDENTIFIER
marcozecchini 0:9fca2b23d0ba 111 * }
marcozecchini 0:9fca2b23d0ba 112 */
marcozecchini 0:9fca2b23d0ba 113 static int pk_write_ec_param( unsigned char **p, unsigned char *start,
marcozecchini 0:9fca2b23d0ba 114 mbedtls_ecp_keypair *ec )
marcozecchini 0:9fca2b23d0ba 115 {
marcozecchini 0:9fca2b23d0ba 116 int ret;
marcozecchini 0:9fca2b23d0ba 117 size_t len = 0;
marcozecchini 0:9fca2b23d0ba 118 const char *oid;
marcozecchini 0:9fca2b23d0ba 119 size_t oid_len;
marcozecchini 0:9fca2b23d0ba 120
marcozecchini 0:9fca2b23d0ba 121 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 122 return( ret );
marcozecchini 0:9fca2b23d0ba 123
marcozecchini 0:9fca2b23d0ba 124 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
marcozecchini 0:9fca2b23d0ba 125
marcozecchini 0:9fca2b23d0ba 126 return( (int) len );
marcozecchini 0:9fca2b23d0ba 127 }
marcozecchini 0:9fca2b23d0ba 128 #endif /* MBEDTLS_ECP_C */
marcozecchini 0:9fca2b23d0ba 129
marcozecchini 0:9fca2b23d0ba 130 int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
marcozecchini 0:9fca2b23d0ba 131 const mbedtls_pk_context *key )
marcozecchini 0:9fca2b23d0ba 132 {
marcozecchini 0:9fca2b23d0ba 133 int ret;
marcozecchini 0:9fca2b23d0ba 134 size_t len = 0;
marcozecchini 0:9fca2b23d0ba 135
marcozecchini 0:9fca2b23d0ba 136 #if defined(MBEDTLS_RSA_C)
marcozecchini 0:9fca2b23d0ba 137 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
marcozecchini 0:9fca2b23d0ba 138 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
marcozecchini 0:9fca2b23d0ba 139 else
marcozecchini 0:9fca2b23d0ba 140 #endif
marcozecchini 0:9fca2b23d0ba 141 #if defined(MBEDTLS_ECP_C)
marcozecchini 0:9fca2b23d0ba 142 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
marcozecchini 0:9fca2b23d0ba 143 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
marcozecchini 0:9fca2b23d0ba 144 else
marcozecchini 0:9fca2b23d0ba 145 #endif
marcozecchini 0:9fca2b23d0ba 146 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
marcozecchini 0:9fca2b23d0ba 147
marcozecchini 0:9fca2b23d0ba 148 return( (int) len );
marcozecchini 0:9fca2b23d0ba 149 }
marcozecchini 0:9fca2b23d0ba 150
marcozecchini 0:9fca2b23d0ba 151 int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
marcozecchini 0:9fca2b23d0ba 152 {
marcozecchini 0:9fca2b23d0ba 153 int ret;
marcozecchini 0:9fca2b23d0ba 154 unsigned char *c;
marcozecchini 0:9fca2b23d0ba 155 size_t len = 0, par_len = 0, oid_len;
marcozecchini 0:9fca2b23d0ba 156 const char *oid;
marcozecchini 0:9fca2b23d0ba 157
marcozecchini 0:9fca2b23d0ba 158 c = buf + size;
marcozecchini 0:9fca2b23d0ba 159
marcozecchini 0:9fca2b23d0ba 160 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
marcozecchini 0:9fca2b23d0ba 161
marcozecchini 0:9fca2b23d0ba 162 if( c - buf < 1 )
marcozecchini 0:9fca2b23d0ba 163 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
marcozecchini 0:9fca2b23d0ba 164
marcozecchini 0:9fca2b23d0ba 165 /*
marcozecchini 0:9fca2b23d0ba 166 * SubjectPublicKeyInfo ::= SEQUENCE {
marcozecchini 0:9fca2b23d0ba 167 * algorithm AlgorithmIdentifier,
marcozecchini 0:9fca2b23d0ba 168 * subjectPublicKey BIT STRING }
marcozecchini 0:9fca2b23d0ba 169 */
marcozecchini 0:9fca2b23d0ba 170 *--c = 0;
marcozecchini 0:9fca2b23d0ba 171 len += 1;
marcozecchini 0:9fca2b23d0ba 172
marcozecchini 0:9fca2b23d0ba 173 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
marcozecchini 0:9fca2b23d0ba 174 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
marcozecchini 0:9fca2b23d0ba 175
marcozecchini 0:9fca2b23d0ba 176 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
marcozecchini 0:9fca2b23d0ba 177 &oid, &oid_len ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 178 {
marcozecchini 0:9fca2b23d0ba 179 return( ret );
marcozecchini 0:9fca2b23d0ba 180 }
marcozecchini 0:9fca2b23d0ba 181
marcozecchini 0:9fca2b23d0ba 182 #if defined(MBEDTLS_ECP_C)
marcozecchini 0:9fca2b23d0ba 183 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
marcozecchini 0:9fca2b23d0ba 184 {
marcozecchini 0:9fca2b23d0ba 185 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
marcozecchini 0:9fca2b23d0ba 186 }
marcozecchini 0:9fca2b23d0ba 187 #endif
marcozecchini 0:9fca2b23d0ba 188
marcozecchini 0:9fca2b23d0ba 189 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
marcozecchini 0:9fca2b23d0ba 190 par_len ) );
marcozecchini 0:9fca2b23d0ba 191
marcozecchini 0:9fca2b23d0ba 192 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
marcozecchini 0:9fca2b23d0ba 193 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
marcozecchini 0:9fca2b23d0ba 194 MBEDTLS_ASN1_SEQUENCE ) );
marcozecchini 0:9fca2b23d0ba 195
marcozecchini 0:9fca2b23d0ba 196 return( (int) len );
marcozecchini 0:9fca2b23d0ba 197 }
marcozecchini 0:9fca2b23d0ba 198
marcozecchini 0:9fca2b23d0ba 199 int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
marcozecchini 0:9fca2b23d0ba 200 {
marcozecchini 0:9fca2b23d0ba 201 int ret;
marcozecchini 0:9fca2b23d0ba 202 unsigned char *c = buf + size;
marcozecchini 0:9fca2b23d0ba 203 size_t len = 0;
marcozecchini 0:9fca2b23d0ba 204
marcozecchini 0:9fca2b23d0ba 205 #if defined(MBEDTLS_RSA_C)
marcozecchini 0:9fca2b23d0ba 206 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
marcozecchini 0:9fca2b23d0ba 207 {
marcozecchini 0:9fca2b23d0ba 208 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
marcozecchini 0:9fca2b23d0ba 209
marcozecchini 0:9fca2b23d0ba 210 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->QP ) );
marcozecchini 0:9fca2b23d0ba 211 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->DQ ) );
marcozecchini 0:9fca2b23d0ba 212 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->DP ) );
marcozecchini 0:9fca2b23d0ba 213 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->Q ) );
marcozecchini 0:9fca2b23d0ba 214 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->P ) );
marcozecchini 0:9fca2b23d0ba 215 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->D ) );
marcozecchini 0:9fca2b23d0ba 216 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->E ) );
marcozecchini 0:9fca2b23d0ba 217 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->N ) );
marcozecchini 0:9fca2b23d0ba 218 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
marcozecchini 0:9fca2b23d0ba 219
marcozecchini 0:9fca2b23d0ba 220 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
marcozecchini 0:9fca2b23d0ba 221 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
marcozecchini 0:9fca2b23d0ba 222 MBEDTLS_ASN1_SEQUENCE ) );
marcozecchini 0:9fca2b23d0ba 223 }
marcozecchini 0:9fca2b23d0ba 224 else
marcozecchini 0:9fca2b23d0ba 225 #endif /* MBEDTLS_RSA_C */
marcozecchini 0:9fca2b23d0ba 226 #if defined(MBEDTLS_ECP_C)
marcozecchini 0:9fca2b23d0ba 227 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
marcozecchini 0:9fca2b23d0ba 228 {
marcozecchini 0:9fca2b23d0ba 229 mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
marcozecchini 0:9fca2b23d0ba 230 size_t pub_len = 0, par_len = 0;
marcozecchini 0:9fca2b23d0ba 231
marcozecchini 0:9fca2b23d0ba 232 /*
marcozecchini 0:9fca2b23d0ba 233 * RFC 5915, or SEC1 Appendix C.4
marcozecchini 0:9fca2b23d0ba 234 *
marcozecchini 0:9fca2b23d0ba 235 * ECPrivateKey ::= SEQUENCE {
marcozecchini 0:9fca2b23d0ba 236 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
marcozecchini 0:9fca2b23d0ba 237 * privateKey OCTET STRING,
marcozecchini 0:9fca2b23d0ba 238 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
marcozecchini 0:9fca2b23d0ba 239 * publicKey [1] BIT STRING OPTIONAL
marcozecchini 0:9fca2b23d0ba 240 * }
marcozecchini 0:9fca2b23d0ba 241 */
marcozecchini 0:9fca2b23d0ba 242
marcozecchini 0:9fca2b23d0ba 243 /* publicKey */
marcozecchini 0:9fca2b23d0ba 244 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
marcozecchini 0:9fca2b23d0ba 245
marcozecchini 0:9fca2b23d0ba 246 if( c - buf < 1 )
marcozecchini 0:9fca2b23d0ba 247 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
marcozecchini 0:9fca2b23d0ba 248 *--c = 0;
marcozecchini 0:9fca2b23d0ba 249 pub_len += 1;
marcozecchini 0:9fca2b23d0ba 250
marcozecchini 0:9fca2b23d0ba 251 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
marcozecchini 0:9fca2b23d0ba 252 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
marcozecchini 0:9fca2b23d0ba 253
marcozecchini 0:9fca2b23d0ba 254 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
marcozecchini 0:9fca2b23d0ba 255 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
marcozecchini 0:9fca2b23d0ba 256 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
marcozecchini 0:9fca2b23d0ba 257 len += pub_len;
marcozecchini 0:9fca2b23d0ba 258
marcozecchini 0:9fca2b23d0ba 259 /* parameters */
marcozecchini 0:9fca2b23d0ba 260 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
marcozecchini 0:9fca2b23d0ba 261
marcozecchini 0:9fca2b23d0ba 262 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
marcozecchini 0:9fca2b23d0ba 263 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
marcozecchini 0:9fca2b23d0ba 264 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
marcozecchini 0:9fca2b23d0ba 265 len += par_len;
marcozecchini 0:9fca2b23d0ba 266
marcozecchini 0:9fca2b23d0ba 267 /* privateKey: write as MPI then fix tag */
marcozecchini 0:9fca2b23d0ba 268 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &ec->d ) );
marcozecchini 0:9fca2b23d0ba 269 *c = MBEDTLS_ASN1_OCTET_STRING;
marcozecchini 0:9fca2b23d0ba 270
marcozecchini 0:9fca2b23d0ba 271 /* version */
marcozecchini 0:9fca2b23d0ba 272 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
marcozecchini 0:9fca2b23d0ba 273
marcozecchini 0:9fca2b23d0ba 274 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
marcozecchini 0:9fca2b23d0ba 275 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
marcozecchini 0:9fca2b23d0ba 276 MBEDTLS_ASN1_SEQUENCE ) );
marcozecchini 0:9fca2b23d0ba 277 }
marcozecchini 0:9fca2b23d0ba 278 else
marcozecchini 0:9fca2b23d0ba 279 #endif /* MBEDTLS_ECP_C */
marcozecchini 0:9fca2b23d0ba 280 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
marcozecchini 0:9fca2b23d0ba 281
marcozecchini 0:9fca2b23d0ba 282 return( (int) len );
marcozecchini 0:9fca2b23d0ba 283 }
marcozecchini 0:9fca2b23d0ba 284
marcozecchini 0:9fca2b23d0ba 285 #if defined(MBEDTLS_PEM_WRITE_C)
marcozecchini 0:9fca2b23d0ba 286
marcozecchini 0:9fca2b23d0ba 287 #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
marcozecchini 0:9fca2b23d0ba 288 #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
marcozecchini 0:9fca2b23d0ba 289
marcozecchini 0:9fca2b23d0ba 290 #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
marcozecchini 0:9fca2b23d0ba 291 #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
marcozecchini 0:9fca2b23d0ba 292 #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
marcozecchini 0:9fca2b23d0ba 293 #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
marcozecchini 0:9fca2b23d0ba 294
marcozecchini 0:9fca2b23d0ba 295 /*
marcozecchini 0:9fca2b23d0ba 296 * Max sizes of key per types. Shown as tag + len (+ content).
marcozecchini 0:9fca2b23d0ba 297 */
marcozecchini 0:9fca2b23d0ba 298
marcozecchini 0:9fca2b23d0ba 299 #if defined(MBEDTLS_RSA_C)
marcozecchini 0:9fca2b23d0ba 300 /*
marcozecchini 0:9fca2b23d0ba 301 * RSA public keys:
marcozecchini 0:9fca2b23d0ba 302 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
marcozecchini 0:9fca2b23d0ba 303 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
marcozecchini 0:9fca2b23d0ba 304 * + 1 + 1 + 9 (rsa oid)
marcozecchini 0:9fca2b23d0ba 305 * + 1 + 1 (params null)
marcozecchini 0:9fca2b23d0ba 306 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
marcozecchini 0:9fca2b23d0ba 307 * RSAPublicKey ::= SEQUENCE { 1 + 3
marcozecchini 0:9fca2b23d0ba 308 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
marcozecchini 0:9fca2b23d0ba 309 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
marcozecchini 0:9fca2b23d0ba 310 * }
marcozecchini 0:9fca2b23d0ba 311 */
marcozecchini 0:9fca2b23d0ba 312 #define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
marcozecchini 0:9fca2b23d0ba 313
marcozecchini 0:9fca2b23d0ba 314 /*
marcozecchini 0:9fca2b23d0ba 315 * RSA private keys:
marcozecchini 0:9fca2b23d0ba 316 * RSAPrivateKey ::= SEQUENCE { 1 + 3
marcozecchini 0:9fca2b23d0ba 317 * version Version, 1 + 1 + 1
marcozecchini 0:9fca2b23d0ba 318 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
marcozecchini 0:9fca2b23d0ba 319 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
marcozecchini 0:9fca2b23d0ba 320 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
marcozecchini 0:9fca2b23d0ba 321 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
marcozecchini 0:9fca2b23d0ba 322 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
marcozecchini 0:9fca2b23d0ba 323 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
marcozecchini 0:9fca2b23d0ba 324 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
marcozecchini 0:9fca2b23d0ba 325 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
marcozecchini 0:9fca2b23d0ba 326 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
marcozecchini 0:9fca2b23d0ba 327 * }
marcozecchini 0:9fca2b23d0ba 328 */
marcozecchini 0:9fca2b23d0ba 329 #define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
marcozecchini 0:9fca2b23d0ba 330 MBEDTLS_MPI_MAX_SIZE % 2
marcozecchini 0:9fca2b23d0ba 331 #define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
marcozecchini 0:9fca2b23d0ba 332 + 5 * MPI_MAX_SIZE_2
marcozecchini 0:9fca2b23d0ba 333
marcozecchini 0:9fca2b23d0ba 334 #else /* MBEDTLS_RSA_C */
marcozecchini 0:9fca2b23d0ba 335
marcozecchini 0:9fca2b23d0ba 336 #define RSA_PUB_DER_MAX_BYTES 0
marcozecchini 0:9fca2b23d0ba 337 #define RSA_PRV_DER_MAX_BYTES 0
marcozecchini 0:9fca2b23d0ba 338
marcozecchini 0:9fca2b23d0ba 339 #endif /* MBEDTLS_RSA_C */
marcozecchini 0:9fca2b23d0ba 340
marcozecchini 0:9fca2b23d0ba 341 #if defined(MBEDTLS_ECP_C)
marcozecchini 0:9fca2b23d0ba 342 /*
marcozecchini 0:9fca2b23d0ba 343 * EC public keys:
marcozecchini 0:9fca2b23d0ba 344 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
marcozecchini 0:9fca2b23d0ba 345 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
marcozecchini 0:9fca2b23d0ba 346 * + 1 + 1 + 7 (ec oid)
marcozecchini 0:9fca2b23d0ba 347 * + 1 + 1 + 9 (namedCurve oid)
marcozecchini 0:9fca2b23d0ba 348 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
marcozecchini 0:9fca2b23d0ba 349 * + 1 (point format) [1]
marcozecchini 0:9fca2b23d0ba 350 * + 2 * ECP_MAX (coords) [1]
marcozecchini 0:9fca2b23d0ba 351 * }
marcozecchini 0:9fca2b23d0ba 352 */
marcozecchini 0:9fca2b23d0ba 353 #define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
marcozecchini 0:9fca2b23d0ba 354
marcozecchini 0:9fca2b23d0ba 355 /*
marcozecchini 0:9fca2b23d0ba 356 * EC private keys:
marcozecchini 0:9fca2b23d0ba 357 * ECPrivateKey ::= SEQUENCE { 1 + 2
marcozecchini 0:9fca2b23d0ba 358 * version INTEGER , 1 + 1 + 1
marcozecchini 0:9fca2b23d0ba 359 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
marcozecchini 0:9fca2b23d0ba 360 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
marcozecchini 0:9fca2b23d0ba 361 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
marcozecchini 0:9fca2b23d0ba 362 * }
marcozecchini 0:9fca2b23d0ba 363 */
marcozecchini 0:9fca2b23d0ba 364 #define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
marcozecchini 0:9fca2b23d0ba 365
marcozecchini 0:9fca2b23d0ba 366 #else /* MBEDTLS_ECP_C */
marcozecchini 0:9fca2b23d0ba 367
marcozecchini 0:9fca2b23d0ba 368 #define ECP_PUB_DER_MAX_BYTES 0
marcozecchini 0:9fca2b23d0ba 369 #define ECP_PRV_DER_MAX_BYTES 0
marcozecchini 0:9fca2b23d0ba 370
marcozecchini 0:9fca2b23d0ba 371 #endif /* MBEDTLS_ECP_C */
marcozecchini 0:9fca2b23d0ba 372
marcozecchini 0:9fca2b23d0ba 373 #define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
marcozecchini 0:9fca2b23d0ba 374 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
marcozecchini 0:9fca2b23d0ba 375 #define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
marcozecchini 0:9fca2b23d0ba 376 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
marcozecchini 0:9fca2b23d0ba 377
marcozecchini 0:9fca2b23d0ba 378 int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
marcozecchini 0:9fca2b23d0ba 379 {
marcozecchini 0:9fca2b23d0ba 380 int ret;
marcozecchini 0:9fca2b23d0ba 381 unsigned char output_buf[PUB_DER_MAX_BYTES];
marcozecchini 0:9fca2b23d0ba 382 size_t olen = 0;
marcozecchini 0:9fca2b23d0ba 383
marcozecchini 0:9fca2b23d0ba 384 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
marcozecchini 0:9fca2b23d0ba 385 sizeof(output_buf) ) ) < 0 )
marcozecchini 0:9fca2b23d0ba 386 {
marcozecchini 0:9fca2b23d0ba 387 return( ret );
marcozecchini 0:9fca2b23d0ba 388 }
marcozecchini 0:9fca2b23d0ba 389
marcozecchini 0:9fca2b23d0ba 390 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
marcozecchini 0:9fca2b23d0ba 391 output_buf + sizeof(output_buf) - ret,
marcozecchini 0:9fca2b23d0ba 392 ret, buf, size, &olen ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 393 {
marcozecchini 0:9fca2b23d0ba 394 return( ret );
marcozecchini 0:9fca2b23d0ba 395 }
marcozecchini 0:9fca2b23d0ba 396
marcozecchini 0:9fca2b23d0ba 397 return( 0 );
marcozecchini 0:9fca2b23d0ba 398 }
marcozecchini 0:9fca2b23d0ba 399
marcozecchini 0:9fca2b23d0ba 400 int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
marcozecchini 0:9fca2b23d0ba 401 {
marcozecchini 0:9fca2b23d0ba 402 int ret;
marcozecchini 0:9fca2b23d0ba 403 unsigned char output_buf[PRV_DER_MAX_BYTES];
marcozecchini 0:9fca2b23d0ba 404 const char *begin, *end;
marcozecchini 0:9fca2b23d0ba 405 size_t olen = 0;
marcozecchini 0:9fca2b23d0ba 406
marcozecchini 0:9fca2b23d0ba 407 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
marcozecchini 0:9fca2b23d0ba 408 return( ret );
marcozecchini 0:9fca2b23d0ba 409
marcozecchini 0:9fca2b23d0ba 410 #if defined(MBEDTLS_RSA_C)
marcozecchini 0:9fca2b23d0ba 411 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
marcozecchini 0:9fca2b23d0ba 412 {
marcozecchini 0:9fca2b23d0ba 413 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
marcozecchini 0:9fca2b23d0ba 414 end = PEM_END_PRIVATE_KEY_RSA;
marcozecchini 0:9fca2b23d0ba 415 }
marcozecchini 0:9fca2b23d0ba 416 else
marcozecchini 0:9fca2b23d0ba 417 #endif
marcozecchini 0:9fca2b23d0ba 418 #if defined(MBEDTLS_ECP_C)
marcozecchini 0:9fca2b23d0ba 419 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
marcozecchini 0:9fca2b23d0ba 420 {
marcozecchini 0:9fca2b23d0ba 421 begin = PEM_BEGIN_PRIVATE_KEY_EC;
marcozecchini 0:9fca2b23d0ba 422 end = PEM_END_PRIVATE_KEY_EC;
marcozecchini 0:9fca2b23d0ba 423 }
marcozecchini 0:9fca2b23d0ba 424 else
marcozecchini 0:9fca2b23d0ba 425 #endif
marcozecchini 0:9fca2b23d0ba 426 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
marcozecchini 0:9fca2b23d0ba 427
marcozecchini 0:9fca2b23d0ba 428 if( ( ret = mbedtls_pem_write_buffer( begin, end,
marcozecchini 0:9fca2b23d0ba 429 output_buf + sizeof(output_buf) - ret,
marcozecchini 0:9fca2b23d0ba 430 ret, buf, size, &olen ) ) != 0 )
marcozecchini 0:9fca2b23d0ba 431 {
marcozecchini 0:9fca2b23d0ba 432 return( ret );
marcozecchini 0:9fca2b23d0ba 433 }
marcozecchini 0:9fca2b23d0ba 434
marcozecchini 0:9fca2b23d0ba 435 return( 0 );
marcozecchini 0:9fca2b23d0ba 436 }
marcozecchini 0:9fca2b23d0ba 437 #endif /* MBEDTLS_PEM_WRITE_C */
marcozecchini 0:9fca2b23d0ba 438
marcozecchini 0:9fca2b23d0ba 439 #endif /* MBEDTLS_PK_WRITE_C */