mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Committer:
Christopher Haster
Date:
Fri Jan 22 16:44:49 2016 -0600
Revision:
1:24750b9ad5ef
Initial move of mbedtls to mercurial

Who changed what in which revision?

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