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