I added functionality to get the RSSI, BER, and Cell Neighbor for reporting connection issues to M2X

Dependencies:   WncControllerK64F

Committer:
JMF
Date:
Thu Nov 17 16:13:29 2016 +0000
Revision:
18:198e9b0acf11
Parent:
12:0071cb144c7a
Updates to mbed os resulted in mutex.h going away and rtos.h needed to be used; This fixes the Mutex typedef failure.  Also cast data buffers from 'char *' to (const std::uint8_t*) to conform with Fred's changes in WncController

Who changed what in which revision?

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