Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ASN.1 buffer writing functionality |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
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 | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 23 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/asn1write.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 25 | #include "mbedtls/error.h" |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 26 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 27 | #include <string.h> |
| 28 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 31 | #else |
| 32 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 33 | #define mbedtls_calloc calloc |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #define mbedtls_free free |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 35 | #endif |
| 36 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 37 | int mbedtls_asn1_write_len( unsigned char **p, const unsigned char *start, size_t len ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 38 | { |
| 39 | if( len < 0x80 ) |
| 40 | { |
| 41 | if( *p - start < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 43 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 44 | *--(*p) = (unsigned char) len; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 45 | return( 1 ); |
| 46 | } |
| 47 | |
| 48 | if( len <= 0xFF ) |
| 49 | { |
| 50 | if( *p - start < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 52 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 53 | *--(*p) = (unsigned char) len; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 54 | *--(*p) = 0x81; |
| 55 | return( 2 ); |
| 56 | } |
| 57 | |
Paul Bakker | c7d6bd4 | 2016-07-14 11:39:56 +0100 | [diff] [blame] | 58 | if( len <= 0xFFFF ) |
| 59 | { |
| 60 | if( *p - start < 3 ) |
| 61 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 62 | |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 63 | *--(*p) = MBEDTLS_BYTE_0( len ); |
| 64 | *--(*p) = MBEDTLS_BYTE_1( len ); |
Paul Bakker | c7d6bd4 | 2016-07-14 11:39:56 +0100 | [diff] [blame] | 65 | *--(*p) = 0x82; |
| 66 | return( 3 ); |
| 67 | } |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 68 | |
Paul Bakker | c7d6bd4 | 2016-07-14 11:39:56 +0100 | [diff] [blame] | 69 | if( len <= 0xFFFFFF ) |
| 70 | { |
| 71 | if( *p - start < 4 ) |
| 72 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 73 | |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 74 | *--(*p) = MBEDTLS_BYTE_0( len ); |
| 75 | *--(*p) = MBEDTLS_BYTE_1( len ); |
| 76 | *--(*p) = MBEDTLS_BYTE_2( len ); |
Paul Bakker | c7d6bd4 | 2016-07-14 11:39:56 +0100 | [diff] [blame] | 77 | *--(*p) = 0x83; |
| 78 | return( 4 ); |
| 79 | } |
| 80 | |
David Horstmann | 8a7629f | 2022-10-06 18:57:57 +0100 | [diff] [blame] | 81 | int len_valid = 1; |
Azim Khan | 45b79cf | 2018-05-23 16:55:16 +0100 | [diff] [blame] | 82 | #if SIZE_MAX > 0xFFFFFFFF |
David Horstmann | 8a7629f | 2022-10-06 18:57:57 +0100 | [diff] [blame] | 83 | len_valid = ( len <= 0xFFFFFFFF ); |
Azim Khan | 45b79cf | 2018-05-23 16:55:16 +0100 | [diff] [blame] | 84 | #endif |
David Horstmann | 8a7629f | 2022-10-06 18:57:57 +0100 | [diff] [blame] | 85 | if( len_valid ) |
Paul Bakker | c7d6bd4 | 2016-07-14 11:39:56 +0100 | [diff] [blame] | 86 | { |
| 87 | if( *p - start < 5 ) |
| 88 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 89 | |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 90 | *--(*p) = MBEDTLS_BYTE_0( len ); |
| 91 | *--(*p) = MBEDTLS_BYTE_1( len ); |
| 92 | *--(*p) = MBEDTLS_BYTE_2( len ); |
| 93 | *--(*p) = MBEDTLS_BYTE_3( len ); |
Paul Bakker | c7d6bd4 | 2016-07-14 11:39:56 +0100 | [diff] [blame] | 94 | *--(*p) = 0x84; |
| 95 | return( 5 ); |
| 96 | } |
David Horstmann | 8a7629f | 2022-10-06 18:57:57 +0100 | [diff] [blame] | 97 | else |
| 98 | { |
| 99 | return( MBEDTLS_ERR_ASN1_INVALID_LENGTH ); |
| 100 | } |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 103 | int mbedtls_asn1_write_tag( unsigned char **p, const unsigned char *start, unsigned char tag ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 104 | { |
| 105 | if( *p - start < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 107 | |
| 108 | *--(*p) = tag; |
| 109 | |
| 110 | return( 1 ); |
| 111 | } |
| 112 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 113 | int mbedtls_asn1_write_raw_buffer( unsigned char **p, const unsigned char *start, |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 114 | const unsigned char *buf, size_t size ) |
| 115 | { |
| 116 | size_t len = 0; |
| 117 | |
Manuel Pégourié-Gonnard | 22c3b7b | 2015-10-21 12:07:47 +0200 | [diff] [blame] | 118 | if( *p < start || (size_t)( *p - start ) < size ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 120 | |
| 121 | len = size; |
| 122 | (*p) -= len; |
| 123 | memcpy( *p, buf, len ); |
| 124 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 125 | return( (int) len ); |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | #if defined(MBEDTLS_BIGNUM_C) |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 129 | int mbedtls_asn1_write_mpi( unsigned char **p, const unsigned char *start, const mbedtls_mpi *X ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 130 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 131 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 132 | size_t len = 0; |
| 133 | |
| 134 | // Write the MPI |
| 135 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | len = mbedtls_mpi_size( X ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 137 | |
Gilles Peskine | 321a089 | 2022-06-10 20:13:33 +0200 | [diff] [blame] | 138 | /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not |
| 139 | * as 0 digits. We need to end up with 020100, not with 0200. */ |
| 140 | if( len == 0 ) |
| 141 | len = 1; |
| 142 | |
Manuel Pégourié-Gonnard | 22c3b7b | 2015-10-21 12:07:47 +0200 | [diff] [blame] | 143 | if( *p < start || (size_t)( *p - start ) < len ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 144 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 145 | |
| 146 | (*p) -= len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 148 | |
| 149 | // DER format assumes 2s complement for numbers, so the leftmost bit |
| 150 | // should be 0 for positive numbers and 1 for negative numbers. |
| 151 | // |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 152 | if( X->s ==1 && **p & 0x80 ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 153 | { |
| 154 | if( *p - start < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 156 | |
| 157 | *--(*p) = 0x00; |
| 158 | len += 1; |
| 159 | } |
| 160 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 162 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 163 | |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 164 | ret = (int) len; |
| 165 | |
| 166 | cleanup: |
| 167 | return( ret ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 168 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 169 | #endif /* MBEDTLS_BIGNUM_C */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 170 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 171 | int mbedtls_asn1_write_null( unsigned char **p, const unsigned char *start ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 172 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 173 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 174 | size_t len = 0; |
| 175 | |
| 176 | // Write NULL |
| 177 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) ); |
| 179 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 180 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 181 | return( (int) len ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 184 | int mbedtls_asn1_write_oid( unsigned char **p, const unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 185 | const char *oid, size_t oid_len ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 186 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 187 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 188 | size_t len = 0; |
| 189 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 190 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 191 | (const unsigned char *) oid, oid_len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) ); |
| 193 | MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 194 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 195 | return( (int) len ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 198 | int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, const unsigned char *start, |
Manuel Pégourié-Gonnard | edda904 | 2013-09-12 02:17:54 +0200 | [diff] [blame] | 199 | const char *oid, size_t oid_len, |
| 200 | size_t par_len ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 201 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 202 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 203 | size_t len = 0; |
| 204 | |
Manuel Pégourié-Gonnard | edda904 | 2013-09-12 02:17:54 +0200 | [diff] [blame] | 205 | if( par_len == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) ); |
Manuel Pégourié-Gonnard | edda904 | 2013-09-12 02:17:54 +0200 | [diff] [blame] | 207 | else |
| 208 | len += par_len; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 209 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 213 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 214 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 215 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 216 | return( (int) len ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 219 | int mbedtls_asn1_write_bool( unsigned char **p, const unsigned char *start, int boolean ) |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 220 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 221 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 222 | size_t len = 0; |
| 223 | |
| 224 | if( *p - start < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 226 | |
Jonathan Leroy | 87c96c2 | 2015-10-14 09:41:56 +0200 | [diff] [blame] | 227 | *--(*p) = (boolean) ? 255 : 0; |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 228 | len++; |
| 229 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 231 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) ); |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 232 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 233 | return( (int) len ); |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 236 | static int asn1_write_tagged_int( unsigned char **p, const unsigned char *start, int val, int tag ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 237 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 238 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 239 | size_t len = 0; |
| 240 | |
Gilles Peskine | 1dbab67 | 2019-03-01 18:15:18 +0100 | [diff] [blame] | 241 | do |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 242 | { |
| 243 | if( *p - start < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Gilles Peskine | 1dbab67 | 2019-03-01 18:15:18 +0100 | [diff] [blame] | 245 | len += 1; |
| 246 | *--(*p) = val & 0xff; |
| 247 | val >>= 8; |
| 248 | } |
| 249 | while( val > 0 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 250 | |
Gilles Peskine | 1dbab67 | 2019-03-01 18:15:18 +0100 | [diff] [blame] | 251 | if( **p & 0x80 ) |
| 252 | { |
| 253 | if( *p - start < 1 ) |
| 254 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 255 | *--(*p) = 0x00; |
| 256 | len += 1; |
| 257 | } |
| 258 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 260 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 261 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 262 | return( (int) len ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 265 | int mbedtls_asn1_write_int( unsigned char **p, const unsigned char *start, int val ) |
Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 266 | { |
| 267 | return( asn1_write_tagged_int( p, start, val, MBEDTLS_ASN1_INTEGER ) ); |
| 268 | } |
| 269 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 270 | int mbedtls_asn1_write_enum( unsigned char **p, const unsigned char *start, int val ) |
Mykhailo Sopiha | 20180ca | 2019-10-29 15:58:10 +0200 | [diff] [blame] | 271 | { |
| 272 | return( asn1_write_tagged_int( p, start, val, MBEDTLS_ASN1_ENUMERATED ) ); |
| 273 | } |
| 274 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 275 | int mbedtls_asn1_write_tagged_string( unsigned char **p, const unsigned char *start, int tag, |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 276 | const char *text, size_t text_len ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 277 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 278 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 279 | size_t len = 0; |
| 280 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 282 | (const unsigned char *) text, text_len ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 283 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 284 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 285 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 286 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 287 | return( (int) len ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 288 | } |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 289 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 290 | int mbedtls_asn1_write_utf8_string( unsigned char **p, const unsigned char *start, |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 291 | const char *text, size_t text_len ) |
| 292 | { |
thomas-dee | eba6c9b | 2018-09-19 09:10:37 +0200 | [diff] [blame] | 293 | return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) ); |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 296 | int mbedtls_asn1_write_printable_string( unsigned char **p, const unsigned char *start, |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 297 | const char *text, size_t text_len ) |
| 298 | { |
thomas-dee | eba6c9b | 2018-09-19 09:10:37 +0200 | [diff] [blame] | 299 | return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) ); |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 302 | int mbedtls_asn1_write_ia5_string( unsigned char **p, const unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 303 | const char *text, size_t text_len ) |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 304 | { |
thomas-dee | eba6c9b | 2018-09-19 09:10:37 +0200 | [diff] [blame] | 305 | return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) ); |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 306 | } |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 307 | |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 308 | int mbedtls_asn1_write_named_bitstring( unsigned char **p, |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 309 | const unsigned char *start, |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 310 | const unsigned char *buf, |
| 311 | size_t bits ) |
| 312 | { |
| 313 | size_t unused_bits, byte_len; |
| 314 | const unsigned char *cur_byte; |
| 315 | unsigned char cur_byte_shifted; |
| 316 | unsigned char bit; |
| 317 | |
| 318 | byte_len = ( bits + 7 ) / 8; |
| 319 | unused_bits = ( byte_len * 8 ) - bits; |
| 320 | |
| 321 | /* |
| 322 | * Named bitstrings require that trailing 0s are excluded in the encoding |
| 323 | * of the bitstring. Trailing 0s are considered part of the 'unused' bits |
| 324 | * when encoding this value in the first content octet |
| 325 | */ |
| 326 | if( bits != 0 ) |
| 327 | { |
| 328 | cur_byte = buf + byte_len - 1; |
| 329 | cur_byte_shifted = *cur_byte >> unused_bits; |
| 330 | |
| 331 | for( ; ; ) |
| 332 | { |
| 333 | bit = cur_byte_shifted & 0x1; |
| 334 | cur_byte_shifted >>= 1; |
| 335 | |
| 336 | if( bit != 0 ) |
| 337 | break; |
| 338 | |
| 339 | bits--; |
| 340 | if( bits == 0 ) |
| 341 | break; |
| 342 | |
| 343 | if( bits % 8 == 0 ) |
| 344 | cur_byte_shifted = *--cur_byte; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return( mbedtls_asn1_write_bitstring( p, start, buf, bits ) ); |
| 349 | } |
| 350 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 351 | int mbedtls_asn1_write_bitstring( unsigned char **p, const unsigned char *start, |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 352 | const unsigned char *buf, size_t bits ) |
| 353 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 354 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 355 | size_t len = 0; |
| 356 | size_t unused_bits, byte_len; |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 357 | |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 358 | byte_len = ( bits + 7 ) / 8; |
| 359 | unused_bits = ( byte_len * 8 ) - bits; |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 360 | |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 361 | if( *p < start || (size_t)( *p - start ) < byte_len + 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 362 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 363 | |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 364 | len = byte_len + 1; |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 365 | |
Andres Amaya Garcia | ec6329f | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 366 | /* Write the bitstring. Ensure the unused bits are zeroed */ |
| 367 | if( byte_len > 0 ) |
| 368 | { |
| 369 | byte_len--; |
| 370 | *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 ); |
| 371 | ( *p ) -= byte_len; |
| 372 | memcpy( *p, buf, byte_len ); |
| 373 | } |
| 374 | |
| 375 | /* Write unused bits */ |
| 376 | *--( *p ) = (unsigned char)unused_bits; |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 377 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 378 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 379 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) ); |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 380 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 381 | return( (int) len ); |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Mateusz Starzyk | 4e300d0 | 2021-01-27 15:37:12 +0100 | [diff] [blame] | 384 | int mbedtls_asn1_write_octet_string( unsigned char **p, const unsigned char *start, |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 385 | const unsigned char *buf, size_t size ) |
| 386 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 387 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 388 | size_t len = 0; |
| 389 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 390 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) ); |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 391 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 392 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 393 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) ); |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 394 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 395 | return( (int) len ); |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 396 | } |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 397 | |
Hanno Becker | 44da18a | 2018-10-12 10:42:13 +0100 | [diff] [blame] | 398 | |
| 399 | /* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(), |
| 400 | * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */ |
| 401 | static mbedtls_asn1_named_data *asn1_find_named_data( |
| 402 | mbedtls_asn1_named_data *list, |
| 403 | const char *oid, size_t len ) |
| 404 | { |
| 405 | while( list != NULL ) |
| 406 | { |
| 407 | if( list->oid.len == len && |
| 408 | memcmp( list->oid.p, oid, len ) == 0 ) |
| 409 | { |
| 410 | break; |
| 411 | } |
| 412 | |
| 413 | list = list->next; |
| 414 | } |
| 415 | |
| 416 | return( list ); |
| 417 | } |
| 418 | |
| 419 | mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( |
| 420 | mbedtls_asn1_named_data **head, |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 421 | const char *oid, size_t oid_len, |
| 422 | const unsigned char *val, |
| 423 | size_t val_len ) |
| 424 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 425 | mbedtls_asn1_named_data *cur; |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 426 | |
Hanno Becker | 44da18a | 2018-10-12 10:42:13 +0100 | [diff] [blame] | 427 | if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL ) |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 428 | { |
| 429 | // Add new entry if not present yet based on OID |
| 430 | // |
Simon Butcher | 2917689 | 2016-05-20 00:19:09 +0100 | [diff] [blame] | 431 | cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1, |
| 432 | sizeof(mbedtls_asn1_named_data) ); |
| 433 | if( cur == NULL ) |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 434 | return( NULL ); |
| 435 | |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 436 | cur->oid.len = oid_len; |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 437 | cur->oid.p = mbedtls_calloc( 1, oid_len ); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 438 | if( cur->oid.p == NULL ) |
| 439 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 440 | mbedtls_free( cur ); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 441 | return( NULL ); |
| 442 | } |
| 443 | |
Manuel Pégourié-Gonnard | e5b0fc1 | 2014-11-12 22:27:42 +0100 | [diff] [blame] | 444 | memcpy( cur->oid.p, oid, oid_len ); |
| 445 | |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 446 | cur->val.len = val_len; |
Gilles Peskine | 09c0a23 | 2019-03-04 15:00:06 +0100 | [diff] [blame] | 447 | if( val_len != 0 ) |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 448 | { |
Gilles Peskine | 09c0a23 | 2019-03-04 15:00:06 +0100 | [diff] [blame] | 449 | cur->val.p = mbedtls_calloc( 1, val_len ); |
| 450 | if( cur->val.p == NULL ) |
| 451 | { |
| 452 | mbedtls_free( cur->oid.p ); |
| 453 | mbedtls_free( cur ); |
| 454 | return( NULL ); |
| 455 | } |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 458 | cur->next = *head; |
| 459 | *head = cur; |
| 460 | } |
Gilles Peskine | 09c0a23 | 2019-03-04 15:00:06 +0100 | [diff] [blame] | 461 | else if( val_len == 0 ) |
| 462 | { |
| 463 | mbedtls_free( cur->val.p ); |
| 464 | cur->val.p = NULL; |
| 465 | } |
| 466 | else if( cur->val.len != val_len ) |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 467 | { |
Manuel Pégourié-Gonnard | 97b5209 | 2015-12-10 10:50:51 +0100 | [diff] [blame] | 468 | /* |
| 469 | * Enlarge existing value buffer if needed |
| 470 | * Preserve old data until the allocation succeeded, to leave list in |
| 471 | * a consistent state in case allocation fails. |
| 472 | */ |
| 473 | void *p = mbedtls_calloc( 1, val_len ); |
| 474 | if( p == NULL ) |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 475 | return( NULL ); |
Manuel Pégourié-Gonnard | 97b5209 | 2015-12-10 10:50:51 +0100 | [diff] [blame] | 476 | |
| 477 | mbedtls_free( cur->val.p ); |
| 478 | cur->val.p = p; |
| 479 | cur->val.len = val_len; |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Werner Lewis | e59a531 | 2022-05-04 09:44:50 +0100 | [diff] [blame] | 482 | if( val != NULL && val_len != 0 ) |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 483 | memcpy( cur->val.p, val, val_len ); |
| 484 | |
| 485 | return( cur ); |
| 486 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 487 | #endif /* MBEDTLS_ASN1_WRITE_C */ |