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