BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:fbdae7e6d805 1 /*
borlanic 0:fbdae7e6d805 2 * ASN.1 buffer writing functionality
borlanic 0:fbdae7e6d805 3 *
borlanic 0:fbdae7e6d805 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
borlanic 0:fbdae7e6d805 5 * SPDX-License-Identifier: Apache-2.0
borlanic 0:fbdae7e6d805 6 *
borlanic 0:fbdae7e6d805 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
borlanic 0:fbdae7e6d805 8 * not use this file except in compliance with the License.
borlanic 0:fbdae7e6d805 9 * You may obtain a copy of the License at
borlanic 0:fbdae7e6d805 10 *
borlanic 0:fbdae7e6d805 11 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:fbdae7e6d805 12 *
borlanic 0:fbdae7e6d805 13 * Unless required by applicable law or agreed to in writing, software
borlanic 0:fbdae7e6d805 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
borlanic 0:fbdae7e6d805 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:fbdae7e6d805 16 * See the License for the specific language governing permissions and
borlanic 0:fbdae7e6d805 17 * limitations under the License.
borlanic 0:fbdae7e6d805 18 *
borlanic 0:fbdae7e6d805 19 * This file is part of mbed TLS (https://tls.mbed.org)
borlanic 0:fbdae7e6d805 20 */
borlanic 0:fbdae7e6d805 21
borlanic 0:fbdae7e6d805 22 #if !defined(MBEDTLS_CONFIG_FILE)
borlanic 0:fbdae7e6d805 23 #include "mbedtls/config.h"
borlanic 0:fbdae7e6d805 24 #else
borlanic 0:fbdae7e6d805 25 #include MBEDTLS_CONFIG_FILE
borlanic 0:fbdae7e6d805 26 #endif
borlanic 0:fbdae7e6d805 27
borlanic 0:fbdae7e6d805 28 #if defined(MBEDTLS_ASN1_WRITE_C)
borlanic 0:fbdae7e6d805 29
borlanic 0:fbdae7e6d805 30 #include "mbedtls/asn1write.h"
borlanic 0:fbdae7e6d805 31
borlanic 0:fbdae7e6d805 32 #include <string.h>
borlanic 0:fbdae7e6d805 33
borlanic 0:fbdae7e6d805 34 #if defined(MBEDTLS_PLATFORM_C)
borlanic 0:fbdae7e6d805 35 #include "mbedtls/platform.h"
borlanic 0:fbdae7e6d805 36 #else
borlanic 0:fbdae7e6d805 37 #include <stdlib.h>
borlanic 0:fbdae7e6d805 38 #define mbedtls_calloc calloc
borlanic 0:fbdae7e6d805 39 #define mbedtls_free free
borlanic 0:fbdae7e6d805 40 #endif
borlanic 0:fbdae7e6d805 41
borlanic 0:fbdae7e6d805 42 int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
borlanic 0:fbdae7e6d805 43 {
borlanic 0:fbdae7e6d805 44 if( len < 0x80 )
borlanic 0:fbdae7e6d805 45 {
borlanic 0:fbdae7e6d805 46 if( *p - start < 1 )
borlanic 0:fbdae7e6d805 47 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
borlanic 0:fbdae7e6d805 48
borlanic 0:fbdae7e6d805 49 *--(*p) = (unsigned char) len;
borlanic 0:fbdae7e6d805 50 return( 1 );
borlanic 0:fbdae7e6d805 51 }
borlanic 0:fbdae7e6d805 52
borlanic 0:fbdae7e6d805 53 if( len <= 0xFF )
borlanic 0:fbdae7e6d805 54 {
borlanic 0:fbdae7e6d805 55 if( *p - start < 2 )
borlanic 0:fbdae7e6d805 56 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
borlanic 0:fbdae7e6d805 57
borlanic 0:fbdae7e6d805 58 *--(*p) = (unsigned char) len;
borlanic 0:fbdae7e6d805 59 *--(*p) = 0x81;
borlanic 0:fbdae7e6d805 60 return( 2 );
borlanic 0:fbdae7e6d805 61 }
borlanic 0:fbdae7e6d805 62
borlanic 0:fbdae7e6d805 63 if( len <= 0xFFFF )
borlanic 0:fbdae7e6d805 64 {
borlanic 0:fbdae7e6d805 65 if( *p - start < 3 )
borlanic 0:fbdae7e6d805 66 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
borlanic 0:fbdae7e6d805 67
borlanic 0:fbdae7e6d805 68 *--(*p) = ( len ) & 0xFF;
borlanic 0:fbdae7e6d805 69 *--(*p) = ( len >> 8 ) & 0xFF;
borlanic 0:fbdae7e6d805 70 *--(*p) = 0x82;
borlanic 0:fbdae7e6d805 71 return( 3 );
borlanic 0:fbdae7e6d805 72 }
borlanic 0:fbdae7e6d805 73
borlanic 0:fbdae7e6d805 74 if( len <= 0xFFFFFF )
borlanic 0:fbdae7e6d805 75 {
borlanic 0:fbdae7e6d805 76 if( *p - start < 4 )
borlanic 0:fbdae7e6d805 77 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
borlanic 0:fbdae7e6d805 78
borlanic 0:fbdae7e6d805 79 *--(*p) = ( len ) & 0xFF;
borlanic 0:fbdae7e6d805 80 *--(*p) = ( len >> 8 ) & 0xFF;
borlanic 0:fbdae7e6d805 81 *--(*p) = ( len >> 16 ) & 0xFF;
borlanic 0:fbdae7e6d805 82 *--(*p) = 0x83;
borlanic 0:fbdae7e6d805 83 return( 4 );
borlanic 0:fbdae7e6d805 84 }
borlanic 0:fbdae7e6d805 85
borlanic 0:fbdae7e6d805 86 if( len <= 0xFFFFFFFF )
borlanic 0:fbdae7e6d805 87 {
borlanic 0:fbdae7e6d805 88 if( *p - start < 5 )
borlanic 0:fbdae7e6d805 89 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
borlanic 0:fbdae7e6d805 90
borlanic 0:fbdae7e6d805 91 *--(*p) = ( len ) & 0xFF;
borlanic 0:fbdae7e6d805 92 *--(*p) = ( len >> 8 ) & 0xFF;
borlanic 0:fbdae7e6d805 93 *--(*p) = ( len >> 16 ) & 0xFF;
borlanic 0:fbdae7e6d805 94 *--(*p) = ( len >> 24 ) & 0xFF;
borlanic 0:fbdae7e6d805 95 *--(*p) = 0x84;
borlanic 0:fbdae7e6d805 96 return( 5 );
borlanic 0:fbdae7e6d805 97 }
borlanic 0:fbdae7e6d805 98
borlanic 0:fbdae7e6d805 99 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
borlanic 0:fbdae7e6d805 100 }
borlanic 0:fbdae7e6d805 101
borlanic 0:fbdae7e6d805 102 int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
borlanic 0:fbdae7e6d805 103 {
borlanic 0:fbdae7e6d805 104 if( *p - start < 1 )
borlanic 0:fbdae7e6d805 105 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
borlanic 0:fbdae7e6d805 106
borlanic 0:fbdae7e6d805 107 *--(*p) = tag;
borlanic 0:fbdae7e6d805 108
borlanic 0:fbdae7e6d805 109 return( 1 );
borlanic 0:fbdae7e6d805 110 }
borlanic 0:fbdae7e6d805 111
borlanic 0:fbdae7e6d805 112 int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
borlanic 0:fbdae7e6d805 113 const unsigned char *buf, size_t size )
borlanic 0:fbdae7e6d805 114 {
borlanic 0:fbdae7e6d805 115 size_t len = 0;
borlanic 0:fbdae7e6d805 116
borlanic 0:fbdae7e6d805 117 if( *p < start || (size_t)( *p - start ) < size )
borlanic 0:fbdae7e6d805 118 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
borlanic 0:fbdae7e6d805 119
borlanic 0:fbdae7e6d805 120 len = size;
borlanic 0:fbdae7e6d805 121 (*p) -= len;
borlanic 0:fbdae7e6d805 122 memcpy( *p, buf, len );
borlanic 0:fbdae7e6d805 123
borlanic 0:fbdae7e6d805 124 return( (int) len );
borlanic 0:fbdae7e6d805 125 }
borlanic 0:fbdae7e6d805 126
borlanic 0:fbdae7e6d805 127 #if defined(MBEDTLS_BIGNUM_C)
borlanic 0:fbdae7e6d805 128 int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
borlanic 0:fbdae7e6d805 129 {
borlanic 0:fbdae7e6d805 130 int ret;
borlanic 0:fbdae7e6d805 131 size_t len = 0;
borlanic 0:fbdae7e6d805 132
borlanic 0:fbdae7e6d805 133 // Write the MPI
borlanic 0:fbdae7e6d805 134 //
borlanic 0:fbdae7e6d805 135 len = mbedtls_mpi_size( X );
borlanic 0:fbdae7e6d805 136
borlanic 0:fbdae7e6d805 137 if( *p < start || (size_t)( *p - start ) < len )
borlanic 0:fbdae7e6d805 138 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
borlanic 0:fbdae7e6d805 139
borlanic 0:fbdae7e6d805 140 (*p) -= len;
borlanic 0:fbdae7e6d805 141 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
borlanic 0:fbdae7e6d805 142
borlanic 0:fbdae7e6d805 143 // DER format assumes 2s complement for numbers, so the leftmost bit
borlanic 0:fbdae7e6d805 144 // should be 0 for positive numbers and 1 for negative numbers.
borlanic 0:fbdae7e6d805 145 //
borlanic 0:fbdae7e6d805 146 if( X->s ==1 && **p & 0x80 )
borlanic 0:fbdae7e6d805 147 {
borlanic 0:fbdae7e6d805 148 if( *p - start < 1 )
borlanic 0:fbdae7e6d805 149 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
borlanic 0:fbdae7e6d805 150
borlanic 0:fbdae7e6d805 151 *--(*p) = 0x00;
borlanic 0:fbdae7e6d805 152 len += 1;
borlanic 0:fbdae7e6d805 153 }
borlanic 0:fbdae7e6d805 154
borlanic 0:fbdae7e6d805 155 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
borlanic 0:fbdae7e6d805 156 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
borlanic 0:fbdae7e6d805 157
borlanic 0:fbdae7e6d805 158 ret = (int) len;
borlanic 0:fbdae7e6d805 159
borlanic 0:fbdae7e6d805 160 cleanup:
borlanic 0:fbdae7e6d805 161 return( ret );
borlanic 0:fbdae7e6d805 162 }
borlanic 0:fbdae7e6d805 163 #endif /* MBEDTLS_BIGNUM_C */
borlanic 0:fbdae7e6d805 164
borlanic 0:fbdae7e6d805 165 int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
borlanic 0:fbdae7e6d805 166 {
borlanic 0:fbdae7e6d805 167 int ret;
borlanic 0:fbdae7e6d805 168 size_t len = 0;
borlanic 0:fbdae7e6d805 169
borlanic 0:fbdae7e6d805 170 // Write NULL
borlanic 0:fbdae7e6d805 171 //
borlanic 0:fbdae7e6d805 172 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
borlanic 0:fbdae7e6d805 173 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
borlanic 0:fbdae7e6d805 174
borlanic 0:fbdae7e6d805 175 return( (int) len );
borlanic 0:fbdae7e6d805 176 }
borlanic 0:fbdae7e6d805 177
borlanic 0:fbdae7e6d805 178 int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
borlanic 0:fbdae7e6d805 179 const char *oid, size_t oid_len )
borlanic 0:fbdae7e6d805 180 {
borlanic 0:fbdae7e6d805 181 int ret;
borlanic 0:fbdae7e6d805 182 size_t len = 0;
borlanic 0:fbdae7e6d805 183
borlanic 0:fbdae7e6d805 184 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
borlanic 0:fbdae7e6d805 185 (const unsigned char *) oid, oid_len ) );
borlanic 0:fbdae7e6d805 186 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
borlanic 0:fbdae7e6d805 187 MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
borlanic 0:fbdae7e6d805 188
borlanic 0:fbdae7e6d805 189 return( (int) len );
borlanic 0:fbdae7e6d805 190 }
borlanic 0:fbdae7e6d805 191
borlanic 0:fbdae7e6d805 192 int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
borlanic 0:fbdae7e6d805 193 const char *oid, size_t oid_len,
borlanic 0:fbdae7e6d805 194 size_t par_len )
borlanic 0:fbdae7e6d805 195 {
borlanic 0:fbdae7e6d805 196 int ret;
borlanic 0:fbdae7e6d805 197 size_t len = 0;
borlanic 0:fbdae7e6d805 198
borlanic 0:fbdae7e6d805 199 if( par_len == 0 )
borlanic 0:fbdae7e6d805 200 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
borlanic 0:fbdae7e6d805 201 else
borlanic 0:fbdae7e6d805 202 len += par_len;
borlanic 0:fbdae7e6d805 203
borlanic 0:fbdae7e6d805 204 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
borlanic 0:fbdae7e6d805 205
borlanic 0:fbdae7e6d805 206 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
borlanic 0:fbdae7e6d805 207 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
borlanic 0:fbdae7e6d805 208 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
borlanic 0:fbdae7e6d805 209
borlanic 0:fbdae7e6d805 210 return( (int) len );
borlanic 0:fbdae7e6d805 211 }
borlanic 0:fbdae7e6d805 212
borlanic 0:fbdae7e6d805 213 int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
borlanic 0:fbdae7e6d805 214 {
borlanic 0:fbdae7e6d805 215 int ret;
borlanic 0:fbdae7e6d805 216 size_t len = 0;
borlanic 0:fbdae7e6d805 217
borlanic 0:fbdae7e6d805 218 if( *p - start < 1 )
borlanic 0:fbdae7e6d805 219 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
borlanic 0:fbdae7e6d805 220
borlanic 0:fbdae7e6d805 221 *--(*p) = (boolean) ? 255 : 0;
borlanic 0:fbdae7e6d805 222 len++;
borlanic 0:fbdae7e6d805 223
borlanic 0:fbdae7e6d805 224 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
borlanic 0:fbdae7e6d805 225 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
borlanic 0:fbdae7e6d805 226
borlanic 0:fbdae7e6d805 227 return( (int) len );
borlanic 0:fbdae7e6d805 228 }
borlanic 0:fbdae7e6d805 229
borlanic 0:fbdae7e6d805 230 int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
borlanic 0:fbdae7e6d805 231 {
borlanic 0:fbdae7e6d805 232 int ret;
borlanic 0:fbdae7e6d805 233 size_t len = 0;
borlanic 0:fbdae7e6d805 234
borlanic 0:fbdae7e6d805 235 // TODO negative values and values larger than 128
borlanic 0:fbdae7e6d805 236 // DER format assumes 2s complement for numbers, so the leftmost bit
borlanic 0:fbdae7e6d805 237 // should be 0 for positive numbers and 1 for negative numbers.
borlanic 0:fbdae7e6d805 238 //
borlanic 0:fbdae7e6d805 239 if( *p - start < 1 )
borlanic 0:fbdae7e6d805 240 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
borlanic 0:fbdae7e6d805 241
borlanic 0:fbdae7e6d805 242 len += 1;
borlanic 0:fbdae7e6d805 243 *--(*p) = val;
borlanic 0:fbdae7e6d805 244
borlanic 0:fbdae7e6d805 245 if( val > 0 && **p & 0x80 )
borlanic 0:fbdae7e6d805 246 {
borlanic 0:fbdae7e6d805 247 if( *p - start < 1 )
borlanic 0:fbdae7e6d805 248 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
borlanic 0:fbdae7e6d805 249
borlanic 0:fbdae7e6d805 250 *--(*p) = 0x00;
borlanic 0:fbdae7e6d805 251 len += 1;
borlanic 0:fbdae7e6d805 252 }
borlanic 0:fbdae7e6d805 253
borlanic 0:fbdae7e6d805 254 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
borlanic 0:fbdae7e6d805 255 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
borlanic 0:fbdae7e6d805 256
borlanic 0:fbdae7e6d805 257 return( (int) len );
borlanic 0:fbdae7e6d805 258 }
borlanic 0:fbdae7e6d805 259
borlanic 0:fbdae7e6d805 260 int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
borlanic 0:fbdae7e6d805 261 const char *text, size_t text_len )
borlanic 0:fbdae7e6d805 262 {
borlanic 0:fbdae7e6d805 263 int ret;
borlanic 0:fbdae7e6d805 264 size_t len = 0;
borlanic 0:fbdae7e6d805 265
borlanic 0:fbdae7e6d805 266 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
borlanic 0:fbdae7e6d805 267 (const unsigned char *) text, text_len ) );
borlanic 0:fbdae7e6d805 268
borlanic 0:fbdae7e6d805 269 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
borlanic 0:fbdae7e6d805 270 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_PRINTABLE_STRING ) );
borlanic 0:fbdae7e6d805 271
borlanic 0:fbdae7e6d805 272 return( (int) len );
borlanic 0:fbdae7e6d805 273 }
borlanic 0:fbdae7e6d805 274
borlanic 0:fbdae7e6d805 275 int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
borlanic 0:fbdae7e6d805 276 const char *text, size_t text_len )
borlanic 0:fbdae7e6d805 277 {
borlanic 0:fbdae7e6d805 278 int ret;
borlanic 0:fbdae7e6d805 279 size_t len = 0;
borlanic 0:fbdae7e6d805 280
borlanic 0:fbdae7e6d805 281 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
borlanic 0:fbdae7e6d805 282 (const unsigned char *) text, text_len ) );
borlanic 0:fbdae7e6d805 283
borlanic 0:fbdae7e6d805 284 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
borlanic 0:fbdae7e6d805 285 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_IA5_STRING ) );
borlanic 0:fbdae7e6d805 286
borlanic 0:fbdae7e6d805 287 return( (int) len );
borlanic 0:fbdae7e6d805 288 }
borlanic 0:fbdae7e6d805 289
borlanic 0:fbdae7e6d805 290 int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
borlanic 0:fbdae7e6d805 291 const unsigned char *buf, size_t bits )
borlanic 0:fbdae7e6d805 292 {
borlanic 0:fbdae7e6d805 293 int ret;
borlanic 0:fbdae7e6d805 294 size_t len = 0, size;
borlanic 0:fbdae7e6d805 295
borlanic 0:fbdae7e6d805 296 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
borlanic 0:fbdae7e6d805 297
borlanic 0:fbdae7e6d805 298 // Calculate byte length
borlanic 0:fbdae7e6d805 299 //
borlanic 0:fbdae7e6d805 300 if( *p < start || (size_t)( *p - start ) < size + 1 )
borlanic 0:fbdae7e6d805 301 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
borlanic 0:fbdae7e6d805 302
borlanic 0:fbdae7e6d805 303 len = size + 1;
borlanic 0:fbdae7e6d805 304 (*p) -= size;
borlanic 0:fbdae7e6d805 305 memcpy( *p, buf, size );
borlanic 0:fbdae7e6d805 306
borlanic 0:fbdae7e6d805 307 // Write unused bits
borlanic 0:fbdae7e6d805 308 //
borlanic 0:fbdae7e6d805 309 *--(*p) = (unsigned char) (size * 8 - bits);
borlanic 0:fbdae7e6d805 310
borlanic 0:fbdae7e6d805 311 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
borlanic 0:fbdae7e6d805 312 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
borlanic 0:fbdae7e6d805 313
borlanic 0:fbdae7e6d805 314 return( (int) len );
borlanic 0:fbdae7e6d805 315 }
borlanic 0:fbdae7e6d805 316
borlanic 0:fbdae7e6d805 317 int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
borlanic 0:fbdae7e6d805 318 const unsigned char *buf, size_t size )
borlanic 0:fbdae7e6d805 319 {
borlanic 0:fbdae7e6d805 320 int ret;
borlanic 0:fbdae7e6d805 321 size_t len = 0;
borlanic 0:fbdae7e6d805 322
borlanic 0:fbdae7e6d805 323 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
borlanic 0:fbdae7e6d805 324
borlanic 0:fbdae7e6d805 325 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
borlanic 0:fbdae7e6d805 326 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
borlanic 0:fbdae7e6d805 327
borlanic 0:fbdae7e6d805 328 return( (int) len );
borlanic 0:fbdae7e6d805 329 }
borlanic 0:fbdae7e6d805 330
borlanic 0:fbdae7e6d805 331 mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **head,
borlanic 0:fbdae7e6d805 332 const char *oid, size_t oid_len,
borlanic 0:fbdae7e6d805 333 const unsigned char *val,
borlanic 0:fbdae7e6d805 334 size_t val_len )
borlanic 0:fbdae7e6d805 335 {
borlanic 0:fbdae7e6d805 336 mbedtls_asn1_named_data *cur;
borlanic 0:fbdae7e6d805 337
borlanic 0:fbdae7e6d805 338 if( ( cur = mbedtls_asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
borlanic 0:fbdae7e6d805 339 {
borlanic 0:fbdae7e6d805 340 // Add new entry if not present yet based on OID
borlanic 0:fbdae7e6d805 341 //
borlanic 0:fbdae7e6d805 342 cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1,
borlanic 0:fbdae7e6d805 343 sizeof(mbedtls_asn1_named_data) );
borlanic 0:fbdae7e6d805 344 if( cur == NULL )
borlanic 0:fbdae7e6d805 345 return( NULL );
borlanic 0:fbdae7e6d805 346
borlanic 0:fbdae7e6d805 347 cur->oid.len = oid_len;
borlanic 0:fbdae7e6d805 348 cur->oid.p = mbedtls_calloc( 1, oid_len );
borlanic 0:fbdae7e6d805 349 if( cur->oid.p == NULL )
borlanic 0:fbdae7e6d805 350 {
borlanic 0:fbdae7e6d805 351 mbedtls_free( cur );
borlanic 0:fbdae7e6d805 352 return( NULL );
borlanic 0:fbdae7e6d805 353 }
borlanic 0:fbdae7e6d805 354
borlanic 0:fbdae7e6d805 355 memcpy( cur->oid.p, oid, oid_len );
borlanic 0:fbdae7e6d805 356
borlanic 0:fbdae7e6d805 357 cur->val.len = val_len;
borlanic 0:fbdae7e6d805 358 cur->val.p = mbedtls_calloc( 1, val_len );
borlanic 0:fbdae7e6d805 359 if( cur->val.p == NULL )
borlanic 0:fbdae7e6d805 360 {
borlanic 0:fbdae7e6d805 361 mbedtls_free( cur->oid.p );
borlanic 0:fbdae7e6d805 362 mbedtls_free( cur );
borlanic 0:fbdae7e6d805 363 return( NULL );
borlanic 0:fbdae7e6d805 364 }
borlanic 0:fbdae7e6d805 365
borlanic 0:fbdae7e6d805 366 cur->next = *head;
borlanic 0:fbdae7e6d805 367 *head = cur;
borlanic 0:fbdae7e6d805 368 }
borlanic 0:fbdae7e6d805 369 else if( cur->val.len < val_len )
borlanic 0:fbdae7e6d805 370 {
borlanic 0:fbdae7e6d805 371 /*
borlanic 0:fbdae7e6d805 372 * Enlarge existing value buffer if needed
borlanic 0:fbdae7e6d805 373 * Preserve old data until the allocation succeeded, to leave list in
borlanic 0:fbdae7e6d805 374 * a consistent state in case allocation fails.
borlanic 0:fbdae7e6d805 375 */
borlanic 0:fbdae7e6d805 376 void *p = mbedtls_calloc( 1, val_len );
borlanic 0:fbdae7e6d805 377 if( p == NULL )
borlanic 0:fbdae7e6d805 378 return( NULL );
borlanic 0:fbdae7e6d805 379
borlanic 0:fbdae7e6d805 380 mbedtls_free( cur->val.p );
borlanic 0:fbdae7e6d805 381 cur->val.p = p;
borlanic 0:fbdae7e6d805 382 cur->val.len = val_len;
borlanic 0:fbdae7e6d805 383 }
borlanic 0:fbdae7e6d805 384
borlanic 0:fbdae7e6d805 385 if( val != NULL )
borlanic 0:fbdae7e6d805 386 memcpy( cur->val.p, val, val_len );
borlanic 0:fbdae7e6d805 387
borlanic 0:fbdae7e6d805 388 return( cur );
borlanic 0:fbdae7e6d805 389 }
borlanic 0:fbdae7e6d805 390 #endif /* MBEDTLS_ASN1_WRITE_C */