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