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