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