blob: 3c411802e65bc73efc326c3e486fd42461ed79b4 [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * ASN.1 buffer writing functionality
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000020 */
21
Gilles Peskinedb09ef62020-06-03 01:43:33 +020022#include "common.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_ASN1_WRITE_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/asn1write.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000027#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020033#else
34#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020035#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define mbedtls_free free
Paul Bakker59ba59f2013-09-09 11:26:00 +020037#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000040{
41 if( len < 0x80 )
42 {
43 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000045
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020046 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000047 return( 1 );
48 }
49
50 if( len <= 0xFF )
51 {
52 if( *p - start < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000054
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020055 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000056 *--(*p) = 0x81;
57 return( 2 );
58 }
59
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010060 if( len <= 0xFFFF )
61 {
62 if( *p - start < 3 )
63 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000064
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010065 *--(*p) = ( len ) & 0xFF;
66 *--(*p) = ( len >> 8 ) & 0xFF;
67 *--(*p) = 0x82;
68 return( 3 );
69 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000070
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010071 if( len <= 0xFFFFFF )
72 {
73 if( *p - start < 4 )
74 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
75
76 *--(*p) = ( len ) & 0xFF;
77 *--(*p) = ( len >> 8 ) & 0xFF;
78 *--(*p) = ( len >> 16 ) & 0xFF;
79 *--(*p) = 0x83;
80 return( 4 );
81 }
82
Azim Khan45b79cf2018-05-23 16:55:16 +010083#if SIZE_MAX > 0xFFFFFFFF
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010084 if( len <= 0xFFFFFFFF )
Azim Khan45b79cf2018-05-23 16:55:16 +010085#endif
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010086 {
87 if( *p - start < 5 )
88 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
89
90 *--(*p) = ( len ) & 0xFF;
91 *--(*p) = ( len >> 8 ) & 0xFF;
92 *--(*p) = ( len >> 16 ) & 0xFF;
93 *--(*p) = ( len >> 24 ) & 0xFF;
94 *--(*p) = 0x84;
95 return( 5 );
96 }
97
Azim Khan45b79cf2018-05-23 16:55:16 +010098#if SIZE_MAX > 0xFFFFFFFF
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010099 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Azim Khan45b79cf2018-05-23 16:55:16 +0100100#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000101}
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000104{
105 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000107
108 *--(*p) = tag;
109
110 return( 1 );
111}
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +0200114 const unsigned char *buf, size_t size )
115{
116 size_t len = 0;
117
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200118 if( *p < start || (size_t)( *p - start ) < size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker9852d002013-08-26 17:56:37 +0200120
121 len = size;
122 (*p) -= len;
123 memcpy( *p, buf, len );
124
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200125 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +0200126}
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#if defined(MBEDTLS_BIGNUM_C)
129int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000130{
Janos Follath24eed8d2019-11-22 13:21:35 +0000131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000132 size_t len = 0;
133
134 // Write the MPI
135 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 len = mbedtls_mpi_size( X );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000137
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200138 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140
141 (*p) -= len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000143
144 // DER format assumes 2s complement for numbers, so the leftmost bit
145 // should be 0 for positive numbers and 1 for negative numbers.
146 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200147 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000148 {
149 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000151
152 *--(*p) = 0x00;
153 len += 1;
154 }
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
157 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000158
Paul Bakker3d8fb632014-04-17 12:42:41 +0200159 ret = (int) len;
160
161cleanup:
162 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000167{
Janos Follath24eed8d2019-11-22 13:21:35 +0000168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000169 size_t len = 0;
170
171 // Write NULL
172 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
174 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000175
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200176 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000177}
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200180 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000181{
Janos Follath24eed8d2019-11-22 13:21:35 +0000182 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000183 size_t len = 0;
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200186 (const unsigned char *) oid, oid_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
188 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000189
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200190 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000191}
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200194 const char *oid, size_t oid_len,
195 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000196{
Janos Follath24eed8d2019-11-22 13:21:35 +0000197 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000198 size_t len = 0;
199
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200200 if( par_len == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200202 else
203 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
208 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
209 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000210
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200211 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000212}
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
Paul Bakker329def32013-09-06 16:34:38 +0200215{
Janos Follath24eed8d2019-11-22 13:21:35 +0000216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker329def32013-09-06 16:34:38 +0200217 size_t len = 0;
218
219 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker329def32013-09-06 16:34:38 +0200221
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200222 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200223 len++;
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
226 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
Paul Bakker329def32013-09-06 16:34:38 +0200227
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200228 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200229}
230
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200231static int asn1_write_tagged_int( unsigned char **p, unsigned char *start, int val, int tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000232{
Janos Follath24eed8d2019-11-22 13:21:35 +0000233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000234 size_t len = 0;
235
Gilles Peskine1dbab672019-03-01 18:15:18 +0100236 do
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000237 {
238 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Gilles Peskine1dbab672019-03-01 18:15:18 +0100240 len += 1;
241 *--(*p) = val & 0xff;
242 val >>= 8;
243 }
244 while( val > 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000245
Gilles Peskine1dbab672019-03-01 18:15:18 +0100246 if( **p & 0x80 )
247 {
248 if( *p - start < 1 )
249 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000250 *--(*p) = 0x00;
251 len += 1;
252 }
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200255 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000256
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200257 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000258}
259
Mykhailo Sopiha20180ca2019-10-29 15:58:10 +0200260int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
261{
262 return( asn1_write_tagged_int( p, start, val, MBEDTLS_ASN1_INTEGER ) );
263}
264
265int mbedtls_asn1_write_enum( unsigned char **p, unsigned char *start, int val )
266{
267 return( asn1_write_tagged_int( p, start, val, MBEDTLS_ASN1_ENUMERATED ) );
268}
269
thomas-deeeba6c9b2018-09-19 09:10:37 +0200270int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100271 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000272{
Janos Follath24eed8d2019-11-22 13:21:35 +0000273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000274 size_t len = 0;
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100277 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100280 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000281
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200282 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000283}
Paul Bakker598e4502013-08-25 14:46:39 +0200284
Jaeden Amero23f954d2018-05-17 11:46:13 +0100285int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
286 const char *text, size_t text_len )
287{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200288 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100289}
290
291int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
292 const char *text, size_t text_len )
293{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200294 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100295}
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200298 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000299{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200300 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
Paul Bakker05888152012-02-16 10:26:57 +0000301}
Paul Bakker598e4502013-08-25 14:46:39 +0200302
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100303int mbedtls_asn1_write_named_bitstring( unsigned char **p,
304 unsigned char *start,
305 const unsigned char *buf,
306 size_t bits )
307{
308 size_t unused_bits, byte_len;
309 const unsigned char *cur_byte;
310 unsigned char cur_byte_shifted;
311 unsigned char bit;
312
313 byte_len = ( bits + 7 ) / 8;
314 unused_bits = ( byte_len * 8 ) - bits;
315
316 /*
317 * Named bitstrings require that trailing 0s are excluded in the encoding
318 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
319 * when encoding this value in the first content octet
320 */
321 if( bits != 0 )
322 {
323 cur_byte = buf + byte_len - 1;
324 cur_byte_shifted = *cur_byte >> unused_bits;
325
326 for( ; ; )
327 {
328 bit = cur_byte_shifted & 0x1;
329 cur_byte_shifted >>= 1;
330
331 if( bit != 0 )
332 break;
333
334 bits--;
335 if( bits == 0 )
336 break;
337
338 if( bits % 8 == 0 )
339 cur_byte_shifted = *--cur_byte;
340 }
341 }
342
343 return( mbedtls_asn1_write_bitstring( p, start, buf, bits ) );
344}
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200347 const unsigned char *buf, size_t bits )
348{
Janos Follath24eed8d2019-11-22 13:21:35 +0000349 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100350 size_t len = 0;
351 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200352
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100353 byte_len = ( bits + 7 ) / 8;
354 unused_bits = ( byte_len * 8 ) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200355
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100356 if( *p < start || (size_t)( *p - start ) < byte_len + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker598e4502013-08-25 14:46:39 +0200358
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100359 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200360
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100361 /* Write the bitstring. Ensure the unused bits are zeroed */
362 if( byte_len > 0 )
363 {
364 byte_len--;
365 *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 );
366 ( *p ) -= byte_len;
367 memcpy( *p, buf, byte_len );
368 }
369
370 /* Write unused bits */
371 *--( *p ) = (unsigned char)unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
374 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200375
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200376 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200377}
378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200380 const unsigned char *buf, size_t size )
381{
Janos Follath24eed8d2019-11-22 13:21:35 +0000382 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200383 size_t len = 0;
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
388 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200389
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200390 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200391}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200392
Hanno Becker44da18a2018-10-12 10:42:13 +0100393
394/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
395 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
396static mbedtls_asn1_named_data *asn1_find_named_data(
397 mbedtls_asn1_named_data *list,
398 const char *oid, size_t len )
399{
400 while( list != NULL )
401 {
402 if( list->oid.len == len &&
403 memcmp( list->oid.p, oid, len ) == 0 )
404 {
405 break;
406 }
407
408 list = list->next;
409 }
410
411 return( list );
412}
413
414mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
415 mbedtls_asn1_named_data **head,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200416 const char *oid, size_t oid_len,
417 const unsigned char *val,
418 size_t val_len )
419{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200421
Hanno Becker44da18a2018-10-12 10:42:13 +0100422 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200423 {
424 // Add new entry if not present yet based on OID
425 //
Simon Butcher29176892016-05-20 00:19:09 +0100426 cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
427 sizeof(mbedtls_asn1_named_data) );
428 if( cur == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200429 return( NULL );
430
Paul Bakker59ba59f2013-09-09 11:26:00 +0200431 cur->oid.len = oid_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200432 cur->oid.p = mbedtls_calloc( 1, oid_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200433 if( cur->oid.p == NULL )
434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200436 return( NULL );
437 }
438
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100439 memcpy( cur->oid.p, oid, oid_len );
440
Paul Bakker59ba59f2013-09-09 11:26:00 +0200441 cur->val.len = val_len;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100442 if( val_len != 0 )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200443 {
Gilles Peskine09c0a232019-03-04 15:00:06 +0100444 cur->val.p = mbedtls_calloc( 1, val_len );
445 if( cur->val.p == NULL )
446 {
447 mbedtls_free( cur->oid.p );
448 mbedtls_free( cur );
449 return( NULL );
450 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200451 }
452
Paul Bakker59ba59f2013-09-09 11:26:00 +0200453 cur->next = *head;
454 *head = cur;
455 }
Gilles Peskine09c0a232019-03-04 15:00:06 +0100456 else if( val_len == 0 )
457 {
458 mbedtls_free( cur->val.p );
459 cur->val.p = NULL;
460 }
461 else if( cur->val.len != val_len )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200462 {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100463 /*
464 * Enlarge existing value buffer if needed
465 * Preserve old data until the allocation succeeded, to leave list in
466 * a consistent state in case allocation fails.
467 */
468 void *p = mbedtls_calloc( 1, val_len );
469 if( p == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200470 return( NULL );
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100471
472 mbedtls_free( cur->val.p );
473 cur->val.p = p;
474 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200475 }
476
477 if( val != NULL )
478 memcpy( cur->val.p, val, val_len );
479
480 return( cur );
481}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#endif /* MBEDTLS_ASN1_WRITE_C */