Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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