This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
timbeight
Date:
Thu May 19 16:02:10 2016 +0000
Revision:
1:0ddbe2d3319c
Parent:
0:f7c60d3e7b8a
This is my first commit while in the class.

Who changed what in which revision?

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