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