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