blob: 89a937b29e54ccb7e5dc851b94c246c0798ea257 [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * ASN.1 buffer writing functionality
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakkerbdb912d2012-02-13 23:11:30 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_ASN1_WRITE_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/asn1write.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000025#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000026
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <string.h>
28
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000032{
33 if( len < 0x80 )
34 {
35 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000037
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020038 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000039 return( 1 );
40 }
41
42 if( len <= 0xFF )
43 {
44 if( *p - start < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000046
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020047 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000048 *--(*p) = 0x81;
49 return( 2 );
50 }
51
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010052 if( len <= 0xFFFF )
53 {
54 if( *p - start < 3 )
55 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000056
Joe Subbianic045dc12021-07-14 12:31:31 +010057 *--(*p) = MBEDTLS_BYTE_0( len );
58 *--(*p) = MBEDTLS_BYTE_1( len );
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010059 *--(*p) = 0x82;
60 return( 3 );
61 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000062
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010063 if( len <= 0xFFFFFF )
64 {
65 if( *p - start < 4 )
66 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
67
Joe Subbianic045dc12021-07-14 12:31:31 +010068 *--(*p) = MBEDTLS_BYTE_0( len );
69 *--(*p) = MBEDTLS_BYTE_1( len );
70 *--(*p) = MBEDTLS_BYTE_2( len );
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010071 *--(*p) = 0x83;
72 return( 4 );
73 }
74
David Horstmann863b17d2022-10-06 18:57:57 +010075 int len_valid = 1;
Azim Khan45b79cf2018-05-23 16:55:16 +010076#if SIZE_MAX > 0xFFFFFFFF
David Horstmann863b17d2022-10-06 18:57:57 +010077 len_valid = ( len <= 0xFFFFFFFF );
Azim Khan45b79cf2018-05-23 16:55:16 +010078#endif
David Horstmann863b17d2022-10-06 18:57:57 +010079 if( len_valid )
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010080 {
81 if( *p - start < 5 )
82 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
83
Joe Subbianic045dc12021-07-14 12:31:31 +010084 *--(*p) = MBEDTLS_BYTE_0( len );
85 *--(*p) = MBEDTLS_BYTE_1( len );
86 *--(*p) = MBEDTLS_BYTE_2( len );
87 *--(*p) = MBEDTLS_BYTE_3( len );
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010088 *--(*p) = 0x84;
89 return( 5 );
90 }
David Horstmann863b17d2022-10-06 18:57:57 +010091 else
92 {
93 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
94 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000095}
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000098{
99 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000101
102 *--(*p) = tag;
103
104 return( 1 );
105}
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +0200108 const unsigned char *buf, size_t size )
109{
110 size_t len = 0;
111
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200112 if( *p < start || (size_t)( *p - start ) < size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker9852d002013-08-26 17:56:37 +0200114
115 len = size;
116 (*p) -= len;
117 memcpy( *p, buf, len );
118
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200119 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +0200120}
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_BIGNUM_C)
123int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000124{
Janos Follath24eed8d2019-11-22 13:21:35 +0000125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000126 size_t len = 0;
127
128 // Write the MPI
129 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 len = mbedtls_mpi_size( X );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000131
Gilles Peskinebb34fee2022-06-10 20:13:33 +0200132 /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
133 * as 0 digits. We need to end up with 020100, not with 0200. */
134 if( len == 0 )
135 len = 1;
136
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200137 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000139
140 (*p) -= len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000142
143 // DER format assumes 2s complement for numbers, so the leftmost bit
144 // should be 0 for positive numbers and 1 for negative numbers.
145 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200146 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147 {
148 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000150
151 *--(*p) = 0x00;
152 len += 1;
153 }
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
156 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000157
Paul Bakker3d8fb632014-04-17 12:42:41 +0200158 ret = (int) len;
159
160cleanup:
161 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000162}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000166{
Janos Follath24eed8d2019-11-22 13:21:35 +0000167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000168 size_t len = 0;
169
170 // Write NULL
171 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
173 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000174
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200175 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000176}
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200179 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000180{
Janos Follath24eed8d2019-11-22 13:21:35 +0000181 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000182 size_t len = 0;
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200185 (const unsigned char *) oid, oid_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
187 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000188
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200189 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000190}
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200193 const char *oid, size_t oid_len,
194 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000195{
Janos Follath24eed8d2019-11-22 13:21:35 +0000196 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000197 size_t len = 0;
198
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200199 if( par_len == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200201 else
202 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
207 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
208 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000209
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200210 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000211}
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
Paul Bakker329def32013-09-06 16:34:38 +0200214{
Janos Follath24eed8d2019-11-22 13:21:35 +0000215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker329def32013-09-06 16:34:38 +0200216 size_t len = 0;
217
218 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker329def32013-09-06 16:34:38 +0200220
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200221 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200222 len++;
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
225 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
Paul Bakker329def32013-09-06 16:34:38 +0200226
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200227 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200228}
229
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200230static int asn1_write_tagged_int( unsigned char **p, unsigned char *start, int val, int tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000231{
Janos Follath24eed8d2019-11-22 13:21:35 +0000232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000233 size_t len = 0;
234
Gilles Peskine1dbab672019-03-01 18:15:18 +0100235 do
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000236 {
237 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Gilles Peskine1dbab672019-03-01 18:15:18 +0100239 len += 1;
240 *--(*p) = val & 0xff;
241 val >>= 8;
242 }
243 while( val > 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000244
Gilles Peskine1dbab672019-03-01 18:15:18 +0100245 if( **p & 0x80 )
246 {
247 if( *p - start < 1 )
248 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000249 *--(*p) = 0x00;
250 len += 1;
251 }
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200254 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000255
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200256 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000257}
258
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200259int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
260{
261 return( asn1_write_tagged_int( p, start, val, MBEDTLS_ASN1_INTEGER ) );
262}
263
264int mbedtls_asn1_write_enum( unsigned char **p, unsigned char *start, int val )
265{
266 return( asn1_write_tagged_int( p, start, val, MBEDTLS_ASN1_ENUMERATED ) );
267}
268
thomas-deeeba6c9b2018-09-19 09:10:37 +0200269int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100270 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000271{
Janos Follath24eed8d2019-11-22 13:21:35 +0000272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000273 size_t len = 0;
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100276 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100279 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000280
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200281 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000282}
Paul Bakker598e4502013-08-25 14:46:39 +0200283
Jaeden Amero23f954d2018-05-17 11:46:13 +0100284int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
285 const char *text, size_t text_len )
286{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200287 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100288}
289
290int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
291 const char *text, size_t text_len )
292{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200293 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100294}
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200297 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000298{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200299 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
Paul Bakker05888152012-02-16 10:26:57 +0000300}
Paul Bakker598e4502013-08-25 14:46:39 +0200301
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100302int mbedtls_asn1_write_named_bitstring( unsigned char **p,
303 unsigned char *start,
304 const unsigned char *buf,
305 size_t bits )
306{
307 size_t unused_bits, byte_len;
308 const unsigned char *cur_byte;
309 unsigned char cur_byte_shifted;
310 unsigned char bit;
311
312 byte_len = ( bits + 7 ) / 8;
313 unused_bits = ( byte_len * 8 ) - bits;
314
315 /*
316 * Named bitstrings require that trailing 0s are excluded in the encoding
317 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
318 * when encoding this value in the first content octet
319 */
320 if( bits != 0 )
321 {
322 cur_byte = buf + byte_len - 1;
323 cur_byte_shifted = *cur_byte >> unused_bits;
324
325 for( ; ; )
326 {
327 bit = cur_byte_shifted & 0x1;
328 cur_byte_shifted >>= 1;
329
330 if( bit != 0 )
331 break;
332
333 bits--;
334 if( bits == 0 )
335 break;
336
337 if( bits % 8 == 0 )
338 cur_byte_shifted = *--cur_byte;
339 }
340 }
341
342 return( mbedtls_asn1_write_bitstring( p, start, buf, bits ) );
343}
344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200346 const unsigned char *buf, size_t bits )
347{
Janos Follath24eed8d2019-11-22 13:21:35 +0000348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100349 size_t len = 0;
350 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200351
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100352 byte_len = ( bits + 7 ) / 8;
353 unused_bits = ( byte_len * 8 ) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200354
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100355 if( *p < start || (size_t)( *p - start ) < byte_len + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker598e4502013-08-25 14:46:39 +0200357
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100358 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200359
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100360 /* Write the bitstring. Ensure the unused bits are zeroed */
361 if( byte_len > 0 )
362 {
363 byte_len--;
364 *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 );
365 ( *p ) -= byte_len;
366 memcpy( *p, buf, byte_len );
367 }
368
369 /* Write unused bits */
370 *--( *p ) = (unsigned char)unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
373 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200374
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200375 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200376}
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200379 const unsigned char *buf, size_t size )
380{
Janos Follath24eed8d2019-11-22 13:21:35 +0000381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200382 size_t len = 0;
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
387 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200388
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200389 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200390}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200391
Hanno Becker44da18a2018-10-12 10:42:13 +0100392
393/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
394 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
395static mbedtls_asn1_named_data *asn1_find_named_data(
396 mbedtls_asn1_named_data *list,
397 const char *oid, size_t len )
398{
399 while( list != NULL )
400 {
401 if( list->oid.len == len &&
402 memcmp( list->oid.p, oid, len ) == 0 )
403 {
404 break;
405 }
406
407 list = list->next;
408 }
409
410 return( list );
411}
412
413mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
414 mbedtls_asn1_named_data **head,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200415 const char *oid, size_t oid_len,
416 const unsigned char *val,
417 size_t val_len )
418{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200420
Hanno Becker44da18a2018-10-12 10:42:13 +0100421 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200422 {
423 // Add new entry if not present yet based on OID
424 //
Simon Butcher29176892016-05-20 00:19:09 +0100425 cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
426 sizeof(mbedtls_asn1_named_data) );
427 if( cur == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200428 return( NULL );
429
Paul Bakker59ba59f2013-09-09 11:26:00 +0200430 cur->oid.len = oid_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200431 cur->oid.p = mbedtls_calloc( 1, oid_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200432 if( cur->oid.p == NULL )
433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200435 return( NULL );
436 }
437
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100438 memcpy( cur->oid.p, oid, oid_len );
439
Paul Bakker59ba59f2013-09-09 11:26:00 +0200440 cur->val.len = val_len;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100441 if( val_len != 0 )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200442 {
Gilles Peskine09c0a232019-03-04 15:00:06 +0100443 cur->val.p = mbedtls_calloc( 1, val_len );
444 if( cur->val.p == NULL )
445 {
446 mbedtls_free( cur->oid.p );
447 mbedtls_free( cur );
448 return( NULL );
449 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200450 }
451
Paul Bakker59ba59f2013-09-09 11:26:00 +0200452 cur->next = *head;
453 *head = cur;
454 }
Gilles Peskine09c0a232019-03-04 15:00:06 +0100455 else if( val_len == 0 )
456 {
457 mbedtls_free( cur->val.p );
458 cur->val.p = NULL;
459 }
460 else if( cur->val.len != val_len )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200461 {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100462 /*
463 * Enlarge existing value buffer if needed
464 * Preserve old data until the allocation succeeded, to leave list in
465 * a consistent state in case allocation fails.
466 */
467 void *p = mbedtls_calloc( 1, val_len );
468 if( p == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200469 return( NULL );
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100470
471 mbedtls_free( cur->val.p );
472 cur->val.p = p;
473 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200474 }
475
Werner Lewis12ddae82022-05-04 09:44:50 +0100476 if( val != NULL && val_len != 0 )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200477 memcpy( cur->val.p, val, val_len );
478
479 return( cur );
480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#endif /* MBEDTLS_ASN1_WRITE_C */