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