mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
features/mbedtls/mbed-crypto/src/asn1write.c@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
- Parent:
- 0:5b88d5760320
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:5b88d5760320 | 1 | /* |
kenjiArai | 0:5b88d5760320 | 2 | * ASN.1 buffer writing functionality |
kenjiArai | 0:5b88d5760320 | 3 | * |
kenjiArai | 0:5b88d5760320 | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
kenjiArai | 0:5b88d5760320 | 5 | * SPDX-License-Identifier: Apache-2.0 |
kenjiArai | 0:5b88d5760320 | 6 | * |
kenjiArai | 0:5b88d5760320 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
kenjiArai | 0:5b88d5760320 | 8 | * not use this file except in compliance with the License. |
kenjiArai | 0:5b88d5760320 | 9 | * You may obtain a copy of the License at |
kenjiArai | 0:5b88d5760320 | 10 | * |
kenjiArai | 0:5b88d5760320 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 0:5b88d5760320 | 12 | * |
kenjiArai | 0:5b88d5760320 | 13 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 0:5b88d5760320 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
kenjiArai | 0:5b88d5760320 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 0:5b88d5760320 | 16 | * See the License for the specific language governing permissions and |
kenjiArai | 0:5b88d5760320 | 17 | * limitations under the License. |
kenjiArai | 0:5b88d5760320 | 18 | * |
kenjiArai | 0:5b88d5760320 | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
kenjiArai | 0:5b88d5760320 | 20 | */ |
kenjiArai | 0:5b88d5760320 | 21 | |
kenjiArai | 0:5b88d5760320 | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
kenjiArai | 0:5b88d5760320 | 23 | #include "mbedtls/config.h" |
kenjiArai | 0:5b88d5760320 | 24 | #else |
kenjiArai | 0:5b88d5760320 | 25 | #include MBEDTLS_CONFIG_FILE |
kenjiArai | 0:5b88d5760320 | 26 | #endif |
kenjiArai | 0:5b88d5760320 | 27 | |
kenjiArai | 0:5b88d5760320 | 28 | #if defined(MBEDTLS_ASN1_WRITE_C) |
kenjiArai | 0:5b88d5760320 | 29 | |
kenjiArai | 0:5b88d5760320 | 30 | #include "mbedtls/asn1write.h" |
kenjiArai | 0:5b88d5760320 | 31 | |
kenjiArai | 0:5b88d5760320 | 32 | #include <string.h> |
kenjiArai | 0:5b88d5760320 | 33 | |
kenjiArai | 0:5b88d5760320 | 34 | #if defined(MBEDTLS_PLATFORM_C) |
kenjiArai | 0:5b88d5760320 | 35 | #include "mbedtls/platform.h" |
kenjiArai | 0:5b88d5760320 | 36 | #else |
kenjiArai | 0:5b88d5760320 | 37 | #include <stdlib.h> |
kenjiArai | 0:5b88d5760320 | 38 | #define mbedtls_calloc calloc |
kenjiArai | 0:5b88d5760320 | 39 | #define mbedtls_free free |
kenjiArai | 0:5b88d5760320 | 40 | #endif |
kenjiArai | 0:5b88d5760320 | 41 | |
kenjiArai | 0:5b88d5760320 | 42 | int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len ) |
kenjiArai | 0:5b88d5760320 | 43 | { |
kenjiArai | 0:5b88d5760320 | 44 | if( len < 0x80 ) |
kenjiArai | 0:5b88d5760320 | 45 | { |
kenjiArai | 0:5b88d5760320 | 46 | if( *p - start < 1 ) |
kenjiArai | 0:5b88d5760320 | 47 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
kenjiArai | 0:5b88d5760320 | 48 | |
kenjiArai | 0:5b88d5760320 | 49 | *--(*p) = (unsigned char) len; |
kenjiArai | 0:5b88d5760320 | 50 | return( 1 ); |
kenjiArai | 0:5b88d5760320 | 51 | } |
kenjiArai | 0:5b88d5760320 | 52 | |
kenjiArai | 0:5b88d5760320 | 53 | if( len <= 0xFF ) |
kenjiArai | 0:5b88d5760320 | 54 | { |
kenjiArai | 0:5b88d5760320 | 55 | if( *p - start < 2 ) |
kenjiArai | 0:5b88d5760320 | 56 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
kenjiArai | 0:5b88d5760320 | 57 | |
kenjiArai | 0:5b88d5760320 | 58 | *--(*p) = (unsigned char) len; |
kenjiArai | 0:5b88d5760320 | 59 | *--(*p) = 0x81; |
kenjiArai | 0:5b88d5760320 | 60 | return( 2 ); |
kenjiArai | 0:5b88d5760320 | 61 | } |
kenjiArai | 0:5b88d5760320 | 62 | |
kenjiArai | 0:5b88d5760320 | 63 | if( len <= 0xFFFF ) |
kenjiArai | 0:5b88d5760320 | 64 | { |
kenjiArai | 0:5b88d5760320 | 65 | if( *p - start < 3 ) |
kenjiArai | 0:5b88d5760320 | 66 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
kenjiArai | 0:5b88d5760320 | 67 | |
kenjiArai | 0:5b88d5760320 | 68 | *--(*p) = ( len ) & 0xFF; |
kenjiArai | 0:5b88d5760320 | 69 | *--(*p) = ( len >> 8 ) & 0xFF; |
kenjiArai | 0:5b88d5760320 | 70 | *--(*p) = 0x82; |
kenjiArai | 0:5b88d5760320 | 71 | return( 3 ); |
kenjiArai | 0:5b88d5760320 | 72 | } |
kenjiArai | 0:5b88d5760320 | 73 | |
kenjiArai | 0:5b88d5760320 | 74 | if( len <= 0xFFFFFF ) |
kenjiArai | 0:5b88d5760320 | 75 | { |
kenjiArai | 0:5b88d5760320 | 76 | if( *p - start < 4 ) |
kenjiArai | 0:5b88d5760320 | 77 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
kenjiArai | 0:5b88d5760320 | 78 | |
kenjiArai | 0:5b88d5760320 | 79 | *--(*p) = ( len ) & 0xFF; |
kenjiArai | 0:5b88d5760320 | 80 | *--(*p) = ( len >> 8 ) & 0xFF; |
kenjiArai | 0:5b88d5760320 | 81 | *--(*p) = ( len >> 16 ) & 0xFF; |
kenjiArai | 0:5b88d5760320 | 82 | *--(*p) = 0x83; |
kenjiArai | 0:5b88d5760320 | 83 | return( 4 ); |
kenjiArai | 0:5b88d5760320 | 84 | } |
kenjiArai | 0:5b88d5760320 | 85 | |
kenjiArai | 0:5b88d5760320 | 86 | #if SIZE_MAX > 0xFFFFFFFF |
kenjiArai | 0:5b88d5760320 | 87 | if( len <= 0xFFFFFFFF ) |
kenjiArai | 0:5b88d5760320 | 88 | #endif |
kenjiArai | 0:5b88d5760320 | 89 | { |
kenjiArai | 0:5b88d5760320 | 90 | if( *p - start < 5 ) |
kenjiArai | 0:5b88d5760320 | 91 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
kenjiArai | 0:5b88d5760320 | 92 | |
kenjiArai | 0:5b88d5760320 | 93 | *--(*p) = ( len ) & 0xFF; |
kenjiArai | 0:5b88d5760320 | 94 | *--(*p) = ( len >> 8 ) & 0xFF; |
kenjiArai | 0:5b88d5760320 | 95 | *--(*p) = ( len >> 16 ) & 0xFF; |
kenjiArai | 0:5b88d5760320 | 96 | *--(*p) = ( len >> 24 ) & 0xFF; |
kenjiArai | 0:5b88d5760320 | 97 | *--(*p) = 0x84; |
kenjiArai | 0:5b88d5760320 | 98 | return( 5 ); |
kenjiArai | 0:5b88d5760320 | 99 | } |
kenjiArai | 0:5b88d5760320 | 100 | |
kenjiArai | 0:5b88d5760320 | 101 | #if SIZE_MAX > 0xFFFFFFFF |
kenjiArai | 0:5b88d5760320 | 102 | return( MBEDTLS_ERR_ASN1_INVALID_LENGTH ); |
kenjiArai | 0:5b88d5760320 | 103 | #endif |
kenjiArai | 0:5b88d5760320 | 104 | } |
kenjiArai | 0:5b88d5760320 | 105 | |
kenjiArai | 0:5b88d5760320 | 106 | int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag ) |
kenjiArai | 0:5b88d5760320 | 107 | { |
kenjiArai | 0:5b88d5760320 | 108 | if( *p - start < 1 ) |
kenjiArai | 0:5b88d5760320 | 109 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
kenjiArai | 0:5b88d5760320 | 110 | |
kenjiArai | 0:5b88d5760320 | 111 | *--(*p) = tag; |
kenjiArai | 0:5b88d5760320 | 112 | |
kenjiArai | 0:5b88d5760320 | 113 | return( 1 ); |
kenjiArai | 0:5b88d5760320 | 114 | } |
kenjiArai | 0:5b88d5760320 | 115 | |
kenjiArai | 0:5b88d5760320 | 116 | int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start, |
kenjiArai | 0:5b88d5760320 | 117 | const unsigned char *buf, size_t size ) |
kenjiArai | 0:5b88d5760320 | 118 | { |
kenjiArai | 0:5b88d5760320 | 119 | size_t len = 0; |
kenjiArai | 0:5b88d5760320 | 120 | |
kenjiArai | 0:5b88d5760320 | 121 | if( *p < start || (size_t)( *p - start ) < size ) |
kenjiArai | 0:5b88d5760320 | 122 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
kenjiArai | 0:5b88d5760320 | 123 | |
kenjiArai | 0:5b88d5760320 | 124 | len = size; |
kenjiArai | 0:5b88d5760320 | 125 | (*p) -= len; |
kenjiArai | 0:5b88d5760320 | 126 | memcpy( *p, buf, len ); |
kenjiArai | 0:5b88d5760320 | 127 | |
kenjiArai | 0:5b88d5760320 | 128 | return( (int) len ); |
kenjiArai | 0:5b88d5760320 | 129 | } |
kenjiArai | 0:5b88d5760320 | 130 | |
kenjiArai | 0:5b88d5760320 | 131 | #if defined(MBEDTLS_BIGNUM_C) |
kenjiArai | 0:5b88d5760320 | 132 | int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X ) |
kenjiArai | 0:5b88d5760320 | 133 | { |
kenjiArai | 0:5b88d5760320 | 134 | int ret; |
kenjiArai | 0:5b88d5760320 | 135 | size_t len = 0; |
kenjiArai | 0:5b88d5760320 | 136 | |
kenjiArai | 0:5b88d5760320 | 137 | // Write the MPI |
kenjiArai | 0:5b88d5760320 | 138 | // |
kenjiArai | 0:5b88d5760320 | 139 | len = mbedtls_mpi_size( X ); |
kenjiArai | 0:5b88d5760320 | 140 | |
kenjiArai | 0:5b88d5760320 | 141 | if( *p < start || (size_t)( *p - start ) < len ) |
kenjiArai | 0:5b88d5760320 | 142 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
kenjiArai | 0:5b88d5760320 | 143 | |
kenjiArai | 0:5b88d5760320 | 144 | (*p) -= len; |
kenjiArai | 0:5b88d5760320 | 145 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) ); |
kenjiArai | 0:5b88d5760320 | 146 | |
kenjiArai | 0:5b88d5760320 | 147 | // DER format assumes 2s complement for numbers, so the leftmost bit |
kenjiArai | 0:5b88d5760320 | 148 | // should be 0 for positive numbers and 1 for negative numbers. |
kenjiArai | 0:5b88d5760320 | 149 | // |
kenjiArai | 0:5b88d5760320 | 150 | if( X->s ==1 && **p & 0x80 ) |
kenjiArai | 0:5b88d5760320 | 151 | { |
kenjiArai | 0:5b88d5760320 | 152 | if( *p - start < 1 ) |
kenjiArai | 0:5b88d5760320 | 153 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
kenjiArai | 0:5b88d5760320 | 154 | |
kenjiArai | 0:5b88d5760320 | 155 | *--(*p) = 0x00; |
kenjiArai | 0:5b88d5760320 | 156 | len += 1; |
kenjiArai | 0:5b88d5760320 | 157 | } |
kenjiArai | 0:5b88d5760320 | 158 | |
kenjiArai | 0:5b88d5760320 | 159 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
kenjiArai | 0:5b88d5760320 | 160 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) ); |
kenjiArai | 0:5b88d5760320 | 161 | |
kenjiArai | 0:5b88d5760320 | 162 | ret = (int) len; |
kenjiArai | 0:5b88d5760320 | 163 | |
kenjiArai | 0:5b88d5760320 | 164 | cleanup: |
kenjiArai | 0:5b88d5760320 | 165 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 166 | } |
kenjiArai | 0:5b88d5760320 | 167 | #endif /* MBEDTLS_BIGNUM_C */ |
kenjiArai | 0:5b88d5760320 | 168 | |
kenjiArai | 0:5b88d5760320 | 169 | int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start ) |
kenjiArai | 0:5b88d5760320 | 170 | { |
kenjiArai | 0:5b88d5760320 | 171 | int ret; |
kenjiArai | 0:5b88d5760320 | 172 | size_t len = 0; |
kenjiArai | 0:5b88d5760320 | 173 | |
kenjiArai | 0:5b88d5760320 | 174 | // Write NULL |
kenjiArai | 0:5b88d5760320 | 175 | // |
kenjiArai | 0:5b88d5760320 | 176 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) ); |
kenjiArai | 0:5b88d5760320 | 177 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) ); |
kenjiArai | 0:5b88d5760320 | 178 | |
kenjiArai | 0:5b88d5760320 | 179 | return( (int) len ); |
kenjiArai | 0:5b88d5760320 | 180 | } |
kenjiArai | 0:5b88d5760320 | 181 | |
kenjiArai | 0:5b88d5760320 | 182 | int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start, |
kenjiArai | 0:5b88d5760320 | 183 | const char *oid, size_t oid_len ) |
kenjiArai | 0:5b88d5760320 | 184 | { |
kenjiArai | 0:5b88d5760320 | 185 | int ret; |
kenjiArai | 0:5b88d5760320 | 186 | size_t len = 0; |
kenjiArai | 0:5b88d5760320 | 187 | |
kenjiArai | 0:5b88d5760320 | 188 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, |
kenjiArai | 0:5b88d5760320 | 189 | (const unsigned char *) oid, oid_len ) ); |
kenjiArai | 0:5b88d5760320 | 190 | MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) ); |
kenjiArai | 0:5b88d5760320 | 191 | MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) ); |
kenjiArai | 0:5b88d5760320 | 192 | |
kenjiArai | 0:5b88d5760320 | 193 | return( (int) len ); |
kenjiArai | 0:5b88d5760320 | 194 | } |
kenjiArai | 0:5b88d5760320 | 195 | |
kenjiArai | 0:5b88d5760320 | 196 | int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start, |
kenjiArai | 0:5b88d5760320 | 197 | const char *oid, size_t oid_len, |
kenjiArai | 0:5b88d5760320 | 198 | size_t par_len ) |
kenjiArai | 0:5b88d5760320 | 199 | { |
kenjiArai | 0:5b88d5760320 | 200 | int ret; |
kenjiArai | 0:5b88d5760320 | 201 | size_t len = 0; |
kenjiArai | 0:5b88d5760320 | 202 | |
kenjiArai | 0:5b88d5760320 | 203 | if( par_len == 0 ) |
kenjiArai | 0:5b88d5760320 | 204 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) ); |
kenjiArai | 0:5b88d5760320 | 205 | else |
kenjiArai | 0:5b88d5760320 | 206 | len += par_len; |
kenjiArai | 0:5b88d5760320 | 207 | |
kenjiArai | 0:5b88d5760320 | 208 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) ); |
kenjiArai | 0:5b88d5760320 | 209 | |
kenjiArai | 0:5b88d5760320 | 210 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
kenjiArai | 0:5b88d5760320 | 211 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
kenjiArai | 0:5b88d5760320 | 212 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ); |
kenjiArai | 0:5b88d5760320 | 213 | |
kenjiArai | 0:5b88d5760320 | 214 | return( (int) len ); |
kenjiArai | 0:5b88d5760320 | 215 | } |
kenjiArai | 0:5b88d5760320 | 216 | |
kenjiArai | 0:5b88d5760320 | 217 | int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean ) |
kenjiArai | 0:5b88d5760320 | 218 | { |
kenjiArai | 0:5b88d5760320 | 219 | int ret; |
kenjiArai | 0:5b88d5760320 | 220 | size_t len = 0; |
kenjiArai | 0:5b88d5760320 | 221 | |
kenjiArai | 0:5b88d5760320 | 222 | if( *p - start < 1 ) |
kenjiArai | 0:5b88d5760320 | 223 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
kenjiArai | 0:5b88d5760320 | 224 | |
kenjiArai | 0:5b88d5760320 | 225 | *--(*p) = (boolean) ? 255 : 0; |
kenjiArai | 0:5b88d5760320 | 226 | len++; |
kenjiArai | 0:5b88d5760320 | 227 | |
kenjiArai | 0:5b88d5760320 | 228 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
kenjiArai | 0:5b88d5760320 | 229 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) ); |
kenjiArai | 0:5b88d5760320 | 230 | |
kenjiArai | 0:5b88d5760320 | 231 | return( (int) len ); |
kenjiArai | 0:5b88d5760320 | 232 | } |
kenjiArai | 0:5b88d5760320 | 233 | |
kenjiArai | 0:5b88d5760320 | 234 | int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val ) |
kenjiArai | 0:5b88d5760320 | 235 | { |
kenjiArai | 0:5b88d5760320 | 236 | int ret; |
kenjiArai | 0:5b88d5760320 | 237 | size_t len = 0; |
kenjiArai | 0:5b88d5760320 | 238 | |
kenjiArai | 1:9db0e321a9f4 | 239 | do |
kenjiArai | 0:5b88d5760320 | 240 | { |
kenjiArai | 0:5b88d5760320 | 241 | if( *p - start < 1 ) |
kenjiArai | 0:5b88d5760320 | 242 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
kenjiArai | 1:9db0e321a9f4 | 243 | len += 1; |
kenjiArai | 1:9db0e321a9f4 | 244 | *--(*p) = val & 0xff; |
kenjiArai | 1:9db0e321a9f4 | 245 | val >>= 8; |
kenjiArai | 1:9db0e321a9f4 | 246 | } |
kenjiArai | 1:9db0e321a9f4 | 247 | while( val > 0 ); |
kenjiArai | 0:5b88d5760320 | 248 | |
kenjiArai | 1:9db0e321a9f4 | 249 | if( **p & 0x80 ) |
kenjiArai | 1:9db0e321a9f4 | 250 | { |
kenjiArai | 1:9db0e321a9f4 | 251 | if( *p - start < 1 ) |
kenjiArai | 1:9db0e321a9f4 | 252 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
kenjiArai | 0:5b88d5760320 | 253 | *--(*p) = 0x00; |
kenjiArai | 0:5b88d5760320 | 254 | len += 1; |
kenjiArai | 0:5b88d5760320 | 255 | } |
kenjiArai | 0:5b88d5760320 | 256 | |
kenjiArai | 0:5b88d5760320 | 257 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
kenjiArai | 0:5b88d5760320 | 258 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) ); |
kenjiArai | 0:5b88d5760320 | 259 | |
kenjiArai | 0:5b88d5760320 | 260 | return( (int) len ); |
kenjiArai | 0:5b88d5760320 | 261 | } |
kenjiArai | 0:5b88d5760320 | 262 | |
kenjiArai | 0:5b88d5760320 | 263 | int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag, |
kenjiArai | 0:5b88d5760320 | 264 | const char *text, size_t text_len ) |
kenjiArai | 0:5b88d5760320 | 265 | { |
kenjiArai | 0:5b88d5760320 | 266 | int ret; |
kenjiArai | 0:5b88d5760320 | 267 | size_t len = 0; |
kenjiArai | 0:5b88d5760320 | 268 | |
kenjiArai | 0:5b88d5760320 | 269 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, |
kenjiArai | 0:5b88d5760320 | 270 | (const unsigned char *) text, text_len ) ); |
kenjiArai | 0:5b88d5760320 | 271 | |
kenjiArai | 0:5b88d5760320 | 272 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
kenjiArai | 0:5b88d5760320 | 273 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) ); |
kenjiArai | 0:5b88d5760320 | 274 | |
kenjiArai | 0:5b88d5760320 | 275 | return( (int) len ); |
kenjiArai | 0:5b88d5760320 | 276 | } |
kenjiArai | 0:5b88d5760320 | 277 | |
kenjiArai | 0:5b88d5760320 | 278 | int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start, |
kenjiArai | 0:5b88d5760320 | 279 | const char *text, size_t text_len ) |
kenjiArai | 0:5b88d5760320 | 280 | { |
kenjiArai | 0:5b88d5760320 | 281 | return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) ); |
kenjiArai | 0:5b88d5760320 | 282 | } |
kenjiArai | 0:5b88d5760320 | 283 | |
kenjiArai | 0:5b88d5760320 | 284 | int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start, |
kenjiArai | 0:5b88d5760320 | 285 | const char *text, size_t text_len ) |
kenjiArai | 0:5b88d5760320 | 286 | { |
kenjiArai | 0:5b88d5760320 | 287 | return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) ); |
kenjiArai | 0:5b88d5760320 | 288 | } |
kenjiArai | 0:5b88d5760320 | 289 | |
kenjiArai | 0:5b88d5760320 | 290 | int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start, |
kenjiArai | 0:5b88d5760320 | 291 | const char *text, size_t text_len ) |
kenjiArai | 0:5b88d5760320 | 292 | { |
kenjiArai | 0:5b88d5760320 | 293 | return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) ); |
kenjiArai | 0:5b88d5760320 | 294 | } |
kenjiArai | 0:5b88d5760320 | 295 | |
kenjiArai | 0:5b88d5760320 | 296 | int mbedtls_asn1_write_named_bitstring( unsigned char **p, |
kenjiArai | 0:5b88d5760320 | 297 | unsigned char *start, |
kenjiArai | 0:5b88d5760320 | 298 | const unsigned char *buf, |
kenjiArai | 0:5b88d5760320 | 299 | size_t bits ) |
kenjiArai | 0:5b88d5760320 | 300 | { |
kenjiArai | 0:5b88d5760320 | 301 | size_t unused_bits, byte_len; |
kenjiArai | 0:5b88d5760320 | 302 | const unsigned char *cur_byte; |
kenjiArai | 0:5b88d5760320 | 303 | unsigned char cur_byte_shifted; |
kenjiArai | 0:5b88d5760320 | 304 | unsigned char bit; |
kenjiArai | 0:5b88d5760320 | 305 | |
kenjiArai | 0:5b88d5760320 | 306 | byte_len = ( bits + 7 ) / 8; |
kenjiArai | 0:5b88d5760320 | 307 | unused_bits = ( byte_len * 8 ) - bits; |
kenjiArai | 0:5b88d5760320 | 308 | |
kenjiArai | 0:5b88d5760320 | 309 | /* |
kenjiArai | 0:5b88d5760320 | 310 | * Named bitstrings require that trailing 0s are excluded in the encoding |
kenjiArai | 0:5b88d5760320 | 311 | * of the bitstring. Trailing 0s are considered part of the 'unused' bits |
kenjiArai | 0:5b88d5760320 | 312 | * when encoding this value in the first content octet |
kenjiArai | 0:5b88d5760320 | 313 | */ |
kenjiArai | 0:5b88d5760320 | 314 | if( bits != 0 ) |
kenjiArai | 0:5b88d5760320 | 315 | { |
kenjiArai | 0:5b88d5760320 | 316 | cur_byte = buf + byte_len - 1; |
kenjiArai | 0:5b88d5760320 | 317 | cur_byte_shifted = *cur_byte >> unused_bits; |
kenjiArai | 0:5b88d5760320 | 318 | |
kenjiArai | 0:5b88d5760320 | 319 | for( ; ; ) |
kenjiArai | 0:5b88d5760320 | 320 | { |
kenjiArai | 0:5b88d5760320 | 321 | bit = cur_byte_shifted & 0x1; |
kenjiArai | 0:5b88d5760320 | 322 | cur_byte_shifted >>= 1; |
kenjiArai | 0:5b88d5760320 | 323 | |
kenjiArai | 0:5b88d5760320 | 324 | if( bit != 0 ) |
kenjiArai | 0:5b88d5760320 | 325 | break; |
kenjiArai | 0:5b88d5760320 | 326 | |
kenjiArai | 0:5b88d5760320 | 327 | bits--; |
kenjiArai | 0:5b88d5760320 | 328 | if( bits == 0 ) |
kenjiArai | 0:5b88d5760320 | 329 | break; |
kenjiArai | 0:5b88d5760320 | 330 | |
kenjiArai | 0:5b88d5760320 | 331 | if( bits % 8 == 0 ) |
kenjiArai | 0:5b88d5760320 | 332 | cur_byte_shifted = *--cur_byte; |
kenjiArai | 0:5b88d5760320 | 333 | } |
kenjiArai | 0:5b88d5760320 | 334 | } |
kenjiArai | 0:5b88d5760320 | 335 | |
kenjiArai | 0:5b88d5760320 | 336 | return( mbedtls_asn1_write_bitstring( p, start, buf, bits ) ); |
kenjiArai | 0:5b88d5760320 | 337 | } |
kenjiArai | 0:5b88d5760320 | 338 | |
kenjiArai | 0:5b88d5760320 | 339 | int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start, |
kenjiArai | 0:5b88d5760320 | 340 | const unsigned char *buf, size_t bits ) |
kenjiArai | 0:5b88d5760320 | 341 | { |
kenjiArai | 0:5b88d5760320 | 342 | int ret; |
kenjiArai | 0:5b88d5760320 | 343 | size_t len = 0; |
kenjiArai | 0:5b88d5760320 | 344 | size_t unused_bits, byte_len; |
kenjiArai | 0:5b88d5760320 | 345 | |
kenjiArai | 0:5b88d5760320 | 346 | byte_len = ( bits + 7 ) / 8; |
kenjiArai | 0:5b88d5760320 | 347 | unused_bits = ( byte_len * 8 ) - bits; |
kenjiArai | 0:5b88d5760320 | 348 | |
kenjiArai | 0:5b88d5760320 | 349 | if( *p < start || (size_t)( *p - start ) < byte_len + 1 ) |
kenjiArai | 0:5b88d5760320 | 350 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
kenjiArai | 0:5b88d5760320 | 351 | |
kenjiArai | 0:5b88d5760320 | 352 | len = byte_len + 1; |
kenjiArai | 0:5b88d5760320 | 353 | |
kenjiArai | 0:5b88d5760320 | 354 | /* Write the bitstring. Ensure the unused bits are zeroed */ |
kenjiArai | 0:5b88d5760320 | 355 | if( byte_len > 0 ) |
kenjiArai | 0:5b88d5760320 | 356 | { |
kenjiArai | 0:5b88d5760320 | 357 | byte_len--; |
kenjiArai | 0:5b88d5760320 | 358 | *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 ); |
kenjiArai | 0:5b88d5760320 | 359 | ( *p ) -= byte_len; |
kenjiArai | 0:5b88d5760320 | 360 | memcpy( *p, buf, byte_len ); |
kenjiArai | 0:5b88d5760320 | 361 | } |
kenjiArai | 0:5b88d5760320 | 362 | |
kenjiArai | 0:5b88d5760320 | 363 | /* Write unused bits */ |
kenjiArai | 0:5b88d5760320 | 364 | *--( *p ) = (unsigned char)unused_bits; |
kenjiArai | 0:5b88d5760320 | 365 | |
kenjiArai | 0:5b88d5760320 | 366 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
kenjiArai | 0:5b88d5760320 | 367 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) ); |
kenjiArai | 0:5b88d5760320 | 368 | |
kenjiArai | 0:5b88d5760320 | 369 | return( (int) len ); |
kenjiArai | 0:5b88d5760320 | 370 | } |
kenjiArai | 0:5b88d5760320 | 371 | |
kenjiArai | 0:5b88d5760320 | 372 | int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start, |
kenjiArai | 0:5b88d5760320 | 373 | const unsigned char *buf, size_t size ) |
kenjiArai | 0:5b88d5760320 | 374 | { |
kenjiArai | 0:5b88d5760320 | 375 | int ret; |
kenjiArai | 0:5b88d5760320 | 376 | size_t len = 0; |
kenjiArai | 0:5b88d5760320 | 377 | |
kenjiArai | 0:5b88d5760320 | 378 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) ); |
kenjiArai | 0:5b88d5760320 | 379 | |
kenjiArai | 0:5b88d5760320 | 380 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
kenjiArai | 0:5b88d5760320 | 381 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) ); |
kenjiArai | 0:5b88d5760320 | 382 | |
kenjiArai | 0:5b88d5760320 | 383 | return( (int) len ); |
kenjiArai | 0:5b88d5760320 | 384 | } |
kenjiArai | 0:5b88d5760320 | 385 | |
kenjiArai | 0:5b88d5760320 | 386 | |
kenjiArai | 0:5b88d5760320 | 387 | /* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(), |
kenjiArai | 0:5b88d5760320 | 388 | * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */ |
kenjiArai | 0:5b88d5760320 | 389 | static mbedtls_asn1_named_data *asn1_find_named_data( |
kenjiArai | 0:5b88d5760320 | 390 | mbedtls_asn1_named_data *list, |
kenjiArai | 0:5b88d5760320 | 391 | const char *oid, size_t len ) |
kenjiArai | 0:5b88d5760320 | 392 | { |
kenjiArai | 0:5b88d5760320 | 393 | while( list != NULL ) |
kenjiArai | 0:5b88d5760320 | 394 | { |
kenjiArai | 0:5b88d5760320 | 395 | if( list->oid.len == len && |
kenjiArai | 0:5b88d5760320 | 396 | memcmp( list->oid.p, oid, len ) == 0 ) |
kenjiArai | 0:5b88d5760320 | 397 | { |
kenjiArai | 0:5b88d5760320 | 398 | break; |
kenjiArai | 0:5b88d5760320 | 399 | } |
kenjiArai | 0:5b88d5760320 | 400 | |
kenjiArai | 0:5b88d5760320 | 401 | list = list->next; |
kenjiArai | 0:5b88d5760320 | 402 | } |
kenjiArai | 0:5b88d5760320 | 403 | |
kenjiArai | 0:5b88d5760320 | 404 | return( list ); |
kenjiArai | 0:5b88d5760320 | 405 | } |
kenjiArai | 0:5b88d5760320 | 406 | |
kenjiArai | 0:5b88d5760320 | 407 | mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( |
kenjiArai | 0:5b88d5760320 | 408 | mbedtls_asn1_named_data **head, |
kenjiArai | 0:5b88d5760320 | 409 | const char *oid, size_t oid_len, |
kenjiArai | 0:5b88d5760320 | 410 | const unsigned char *val, |
kenjiArai | 0:5b88d5760320 | 411 | size_t val_len ) |
kenjiArai | 0:5b88d5760320 | 412 | { |
kenjiArai | 0:5b88d5760320 | 413 | mbedtls_asn1_named_data *cur; |
kenjiArai | 0:5b88d5760320 | 414 | |
kenjiArai | 0:5b88d5760320 | 415 | if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL ) |
kenjiArai | 0:5b88d5760320 | 416 | { |
kenjiArai | 0:5b88d5760320 | 417 | // Add new entry if not present yet based on OID |
kenjiArai | 0:5b88d5760320 | 418 | // |
kenjiArai | 0:5b88d5760320 | 419 | cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1, |
kenjiArai | 0:5b88d5760320 | 420 | sizeof(mbedtls_asn1_named_data) ); |
kenjiArai | 0:5b88d5760320 | 421 | if( cur == NULL ) |
kenjiArai | 0:5b88d5760320 | 422 | return( NULL ); |
kenjiArai | 0:5b88d5760320 | 423 | |
kenjiArai | 0:5b88d5760320 | 424 | cur->oid.len = oid_len; |
kenjiArai | 0:5b88d5760320 | 425 | cur->oid.p = mbedtls_calloc( 1, oid_len ); |
kenjiArai | 0:5b88d5760320 | 426 | if( cur->oid.p == NULL ) |
kenjiArai | 0:5b88d5760320 | 427 | { |
kenjiArai | 0:5b88d5760320 | 428 | mbedtls_free( cur ); |
kenjiArai | 0:5b88d5760320 | 429 | return( NULL ); |
kenjiArai | 0:5b88d5760320 | 430 | } |
kenjiArai | 0:5b88d5760320 | 431 | |
kenjiArai | 0:5b88d5760320 | 432 | memcpy( cur->oid.p, oid, oid_len ); |
kenjiArai | 0:5b88d5760320 | 433 | |
kenjiArai | 0:5b88d5760320 | 434 | cur->val.len = val_len; |
kenjiArai | 1:9db0e321a9f4 | 435 | if( val_len != 0 ) |
kenjiArai | 0:5b88d5760320 | 436 | { |
kenjiArai | 1:9db0e321a9f4 | 437 | cur->val.p = mbedtls_calloc( 1, val_len ); |
kenjiArai | 1:9db0e321a9f4 | 438 | if( cur->val.p == NULL ) |
kenjiArai | 1:9db0e321a9f4 | 439 | { |
kenjiArai | 1:9db0e321a9f4 | 440 | mbedtls_free( cur->oid.p ); |
kenjiArai | 1:9db0e321a9f4 | 441 | mbedtls_free( cur ); |
kenjiArai | 1:9db0e321a9f4 | 442 | return( NULL ); |
kenjiArai | 1:9db0e321a9f4 | 443 | } |
kenjiArai | 0:5b88d5760320 | 444 | } |
kenjiArai | 0:5b88d5760320 | 445 | |
kenjiArai | 0:5b88d5760320 | 446 | cur->next = *head; |
kenjiArai | 0:5b88d5760320 | 447 | *head = cur; |
kenjiArai | 0:5b88d5760320 | 448 | } |
kenjiArai | 1:9db0e321a9f4 | 449 | else if( val_len == 0 ) |
kenjiArai | 1:9db0e321a9f4 | 450 | { |
kenjiArai | 1:9db0e321a9f4 | 451 | mbedtls_free( cur->val.p ); |
kenjiArai | 1:9db0e321a9f4 | 452 | cur->val.p = NULL; |
kenjiArai | 1:9db0e321a9f4 | 453 | } |
kenjiArai | 1:9db0e321a9f4 | 454 | else if( cur->val.len != val_len ) |
kenjiArai | 0:5b88d5760320 | 455 | { |
kenjiArai | 0:5b88d5760320 | 456 | /* |
kenjiArai | 0:5b88d5760320 | 457 | * Enlarge existing value buffer if needed |
kenjiArai | 0:5b88d5760320 | 458 | * Preserve old data until the allocation succeeded, to leave list in |
kenjiArai | 0:5b88d5760320 | 459 | * a consistent state in case allocation fails. |
kenjiArai | 0:5b88d5760320 | 460 | */ |
kenjiArai | 0:5b88d5760320 | 461 | void *p = mbedtls_calloc( 1, val_len ); |
kenjiArai | 0:5b88d5760320 | 462 | if( p == NULL ) |
kenjiArai | 0:5b88d5760320 | 463 | return( NULL ); |
kenjiArai | 0:5b88d5760320 | 464 | |
kenjiArai | 0:5b88d5760320 | 465 | mbedtls_free( cur->val.p ); |
kenjiArai | 0:5b88d5760320 | 466 | cur->val.p = p; |
kenjiArai | 0:5b88d5760320 | 467 | cur->val.len = val_len; |
kenjiArai | 0:5b88d5760320 | 468 | } |
kenjiArai | 0:5b88d5760320 | 469 | |
kenjiArai | 0:5b88d5760320 | 470 | if( val != NULL ) |
kenjiArai | 0:5b88d5760320 | 471 | memcpy( cur->val.p, val, val_len ); |
kenjiArai | 0:5b88d5760320 | 472 | |
kenjiArai | 0:5b88d5760320 | 473 | return( cur ); |
kenjiArai | 0:5b88d5760320 | 474 | } |
kenjiArai | 0:5b88d5760320 | 475 | #endif /* MBEDTLS_ASN1_WRITE_C */ |