blob: 262d0bf561da9eb7f8812b5f1594d081b1ee613a [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_ASN1_WRITE_C)
Paul Bakkerbdb912d2012-02-13 23:11:30 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/asn1write.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/platform.h"
Paul Bakker59ba59f2013-09-09 11:26:00 +020037#else
38#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020039#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define mbedtls_free free
Paul Bakker59ba59f2013-09-09 11:26:00 +020041#endif
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +000044{
45 if( len < 0x80 )
46 {
47 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000049
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020050 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000051 return( 1 );
52 }
53
54 if( len <= 0xFF )
55 {
56 if( *p - start < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000058
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020059 *--(*p) = (unsigned char) len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +000060 *--(*p) = 0x81;
61 return( 2 );
62 }
63
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010064 if( len <= 0xFFFF )
65 {
66 if( *p - start < 3 )
67 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +000068
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010069 *--(*p) = ( len ) & 0xFF;
70 *--(*p) = ( len >> 8 ) & 0xFF;
71 *--(*p) = 0x82;
72 return( 3 );
73 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +000074
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010075 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 Khan45b79cf2018-05-23 16:55:16 +010087#if SIZE_MAX > 0xFFFFFFFF
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010088 if( len <= 0xFFFFFFFF )
Azim Khan45b79cf2018-05-23 16:55:16 +010089#endif
Paul Bakkerc7d6bd42016-07-14 11:39:56 +010090 {
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 Khan45b79cf2018-05-23 16:55:16 +0100102#if SIZE_MAX > 0xFFFFFFFF
Paul Bakkerc7d6bd42016-07-14 11:39:56 +0100103 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Azim Khan45b79cf2018-05-23 16:55:16 +0100104#endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000105}
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000108{
109 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000111
112 *--(*p) = tag;
113
114 return( 1 );
115}
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
Paul Bakker9852d002013-08-26 17:56:37 +0200118 const unsigned char *buf, size_t size )
119{
120 size_t len = 0;
121
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200122 if( *p < start || (size_t)( *p - start ) < size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker9852d002013-08-26 17:56:37 +0200124
125 len = size;
126 (*p) -= len;
127 memcpy( *p, buf, len );
128
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200129 return( (int) len );
Paul Bakker9852d002013-08-26 17:56:37 +0200130}
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_BIGNUM_C)
133int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000134{
Janos Follath24eed8d2019-11-22 13:21:35 +0000135 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000136 size_t len = 0;
137
138 // Write the MPI
139 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 len = mbedtls_mpi_size( X );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000141
Manuel Pégourié-Gonnard22c3b7b2015-10-21 12:07:47 +0200142 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000144
145 (*p) -= len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147
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 Bakker66d5d072014-06-17 16:39:18 +0200151 if( X->s ==1 && **p & 0x80 )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000152 {
153 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000155
156 *--(*p) = 0x00;
157 len += 1;
158 }
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 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 Bakkerbdb912d2012-02-13 23:11:30 +0000162
Paul Bakker3d8fb632014-04-17 12:42:41 +0200163 ret = (int) len;
164
165cleanup:
166 return( ret );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000167}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_BIGNUM_C */
Paul Bakkered27a042013-04-18 22:46:23 +0200169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000171{
Janos Follath24eed8d2019-11-22 13:21:35 +0000172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000173 size_t len = 0;
174
175 // Write NULL
176 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 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 Bakkerbdb912d2012-02-13 23:11:30 +0000179
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200180 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000181}
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200184 const char *oid, size_t oid_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000185{
Janos Follath24eed8d2019-11-22 13:21:35 +0000186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000187 size_t len = 0;
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200190 (const unsigned char *) oid, oid_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 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 Bakkerbdb912d2012-02-13 23:11:30 +0000193
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200194 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000195}
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200198 const char *oid, size_t oid_len,
199 size_t par_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000200{
Janos Follath24eed8d2019-11-22 13:21:35 +0000201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000202 size_t len = 0;
203
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200204 if( par_len == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
Manuel Pégourié-Gonnardedda9042013-09-12 02:17:54 +0200206 else
207 len += par_len;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 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 Bakkerbdb912d2012-02-13 23:11:30 +0000214
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200215 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000216}
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
Paul Bakker329def32013-09-06 16:34:38 +0200219{
Janos Follath24eed8d2019-11-22 13:21:35 +0000220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker329def32013-09-06 16:34:38 +0200221 size_t len = 0;
222
223 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker329def32013-09-06 16:34:38 +0200225
Jonathan Leroy87c96c22015-10-14 09:41:56 +0200226 *--(*p) = (boolean) ? 255 : 0;
Paul Bakker329def32013-09-06 16:34:38 +0200227 len++;
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 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 Bakker329def32013-09-06 16:34:38 +0200231
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200232 return( (int) len );
Paul Bakker329def32013-09-06 16:34:38 +0200233}
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000236{
Janos Follath24eed8d2019-11-22 13:21:35 +0000237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000238 size_t len = 0;
239
Gilles Peskine1dbab672019-03-01 18:15:18 +0100240 do
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000241 {
242 if( *p - start < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Gilles Peskine1dbab672019-03-01 18:15:18 +0100244 len += 1;
245 *--(*p) = val & 0xff;
246 val >>= 8;
247 }
248 while( val > 0 );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000249
Gilles Peskine1dbab672019-03-01 18:15:18 +0100250 if( **p & 0x80 )
251 {
252 if( *p - start < 1 )
253 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000254 *--(*p) = 0x00;
255 len += 1;
256 }
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
259 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000260
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200261 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000262}
263
thomas-deeeba6c9b2018-09-19 09:10:37 +0200264int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100265 const char *text, size_t text_len )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000266{
Janos Follath24eed8d2019-11-22 13:21:35 +0000267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000268 size_t len = 0;
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Jaeden Amero23f954d2018-05-17 11:46:13 +0100271 (const unsigned char *) text, text_len ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100274 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000275
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200276 return( (int) len );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000277}
Paul Bakker598e4502013-08-25 14:46:39 +0200278
Jaeden Amero23f954d2018-05-17 11:46:13 +0100279int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start,
280 const char *text, size_t text_len )
281{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200282 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100283}
284
285int mbedtls_asn1_write_printable_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_PRINTABLE_STRING, text, text_len) );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100289}
290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
Paul Bakker5f45e622013-09-09 12:02:36 +0200292 const char *text, size_t text_len )
Paul Bakker05888152012-02-16 10:26:57 +0000293{
thomas-deeeba6c9b2018-09-19 09:10:37 +0200294 return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) );
Paul Bakker05888152012-02-16 10:26:57 +0000295}
Paul Bakker598e4502013-08-25 14:46:39 +0200296
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100297int mbedtls_asn1_write_named_bitstring( unsigned char **p,
298 unsigned char *start,
299 const unsigned char *buf,
300 size_t bits )
301{
302 size_t unused_bits, byte_len;
303 const unsigned char *cur_byte;
304 unsigned char cur_byte_shifted;
305 unsigned char bit;
306
307 byte_len = ( bits + 7 ) / 8;
308 unused_bits = ( byte_len * 8 ) - bits;
309
310 /*
311 * Named bitstrings require that trailing 0s are excluded in the encoding
312 * of the bitstring. Trailing 0s are considered part of the 'unused' bits
313 * when encoding this value in the first content octet
314 */
315 if( bits != 0 )
316 {
317 cur_byte = buf + byte_len - 1;
318 cur_byte_shifted = *cur_byte >> unused_bits;
319
320 for( ; ; )
321 {
322 bit = cur_byte_shifted & 0x1;
323 cur_byte_shifted >>= 1;
324
325 if( bit != 0 )
326 break;
327
328 bits--;
329 if( bits == 0 )
330 break;
331
332 if( bits % 8 == 0 )
333 cur_byte_shifted = *--cur_byte;
334 }
335 }
336
337 return( mbedtls_asn1_write_bitstring( p, start, buf, bits ) );
338}
339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200341 const unsigned char *buf, size_t bits )
342{
Janos Follath24eed8d2019-11-22 13:21:35 +0000343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100344 size_t len = 0;
345 size_t unused_bits, byte_len;
Paul Bakker598e4502013-08-25 14:46:39 +0200346
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100347 byte_len = ( bits + 7 ) / 8;
348 unused_bits = ( byte_len * 8 ) - bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200349
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100350 if( *p < start || (size_t)( *p - start ) < byte_len + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker598e4502013-08-25 14:46:39 +0200352
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100353 len = byte_len + 1;
Paul Bakker598e4502013-08-25 14:46:39 +0200354
Andres Amaya Garciaec6329f2018-09-26 10:48:24 +0100355 /* Write the bitstring. Ensure the unused bits are zeroed */
356 if( byte_len > 0 )
357 {
358 byte_len--;
359 *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 );
360 ( *p ) -= byte_len;
361 memcpy( *p, buf, byte_len );
362 }
363
364 /* Write unused bits */
365 *--( *p ) = (unsigned char)unused_bits;
Paul Bakker598e4502013-08-25 14:46:39 +0200366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
368 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200369
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200370 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200371}
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
Paul Bakker598e4502013-08-25 14:46:39 +0200374 const unsigned char *buf, size_t size )
375{
Janos Follath24eed8d2019-11-22 13:21:35 +0000376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker598e4502013-08-25 14:46:39 +0200377 size_t len = 0;
378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
382 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker598e4502013-08-25 14:46:39 +0200383
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200384 return( (int) len );
Paul Bakker598e4502013-08-25 14:46:39 +0200385}
Paul Bakker59ba59f2013-09-09 11:26:00 +0200386
Hanno Becker44da18a2018-10-12 10:42:13 +0100387
388/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
389 * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
390static mbedtls_asn1_named_data *asn1_find_named_data(
391 mbedtls_asn1_named_data *list,
392 const char *oid, size_t len )
393{
394 while( list != NULL )
395 {
396 if( list->oid.len == len &&
397 memcmp( list->oid.p, oid, len ) == 0 )
398 {
399 break;
400 }
401
402 list = list->next;
403 }
404
405 return( list );
406}
407
408mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
409 mbedtls_asn1_named_data **head,
Paul Bakker59ba59f2013-09-09 11:26:00 +0200410 const char *oid, size_t oid_len,
411 const unsigned char *val,
412 size_t val_len )
413{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_asn1_named_data *cur;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200415
Hanno Becker44da18a2018-10-12 10:42:13 +0100416 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200417 {
418 // Add new entry if not present yet based on OID
419 //
Simon Butcher29176892016-05-20 00:19:09 +0100420 cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
421 sizeof(mbedtls_asn1_named_data) );
422 if( cur == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200423 return( NULL );
424
Paul Bakker59ba59f2013-09-09 11:26:00 +0200425 cur->oid.len = oid_len;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200426 cur->oid.p = mbedtls_calloc( 1, oid_len );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200427 if( cur->oid.p == NULL )
428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_free( cur );
Paul Bakker59ba59f2013-09-09 11:26:00 +0200430 return( NULL );
431 }
432
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100433 memcpy( cur->oid.p, oid, oid_len );
434
Paul Bakker59ba59f2013-09-09 11:26:00 +0200435 cur->val.len = val_len;
Gilles Peskine09c0a232019-03-04 15:00:06 +0100436 if( val_len != 0 )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200437 {
Gilles Peskine09c0a232019-03-04 15:00:06 +0100438 cur->val.p = mbedtls_calloc( 1, val_len );
439 if( cur->val.p == NULL )
440 {
441 mbedtls_free( cur->oid.p );
442 mbedtls_free( cur );
443 return( NULL );
444 }
Paul Bakker59ba59f2013-09-09 11:26:00 +0200445 }
446
Paul Bakker59ba59f2013-09-09 11:26:00 +0200447 cur->next = *head;
448 *head = cur;
449 }
Gilles Peskine09c0a232019-03-04 15:00:06 +0100450 else if( val_len == 0 )
451 {
452 mbedtls_free( cur->val.p );
453 cur->val.p = NULL;
454 }
455 else if( cur->val.len != val_len )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200456 {
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100457 /*
458 * Enlarge existing value buffer if needed
459 * Preserve old data until the allocation succeeded, to leave list in
460 * a consistent state in case allocation fails.
461 */
462 void *p = mbedtls_calloc( 1, val_len );
463 if( p == NULL )
Paul Bakker59ba59f2013-09-09 11:26:00 +0200464 return( NULL );
Manuel Pégourié-Gonnard97b52092015-12-10 10:50:51 +0100465
466 mbedtls_free( cur->val.p );
467 cur->val.p = p;
468 cur->val.len = val_len;
Paul Bakker59ba59f2013-09-09 11:26:00 +0200469 }
470
471 if( val != NULL )
472 memcpy( cur->val.p, val, val_len );
473
474 return( cur );
475}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#endif /* MBEDTLS_ASN1_WRITE_C */