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