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