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