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