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