This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
timbeight
Date:
Thu May 19 16:02:10 2016 +0000
Revision:
1:0ddbe2d3319c
Parent:
0:f7c60d3e7b8a
This is my first commit while in the class.

Who changed what in which revision?

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