mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
Jasper Wallace
Date:
Fri Sep 29 19:50:30 2017 +0100
Revision:
2:bbdeda018a3c
Parent:
0:cdf462088d13
Update to mbedtls 2.6.0, many changes.

Changes to mbedtls sources made:

in include/mbedtls/config.h comment out:

#define MBEDTLS_FS_IO
#define MBEDTLS_NET_C
#define MBEDTLS_TIMING_C

uncomment:

#define MBEDTLS_NO_PLATFORM_ENTROPY

remove the following directorys:

programs
yotta
visualc

Who changed what in which revision?

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