nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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