Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

Committer:
HannesTschofenig
Date:
Thu Sep 27 06:34:22 2018 +0000
Revision:
0:796d0f61a05b
Example AES-GCM test program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HannesTschofenig 0:796d0f61a05b 1 /*
HannesTschofenig 0:796d0f61a05b 2 * ASN.1 buffer writing functionality
HannesTschofenig 0:796d0f61a05b 3 *
HannesTschofenig 0:796d0f61a05b 4 * Copyright (C) 2006-2014, Brainspark B.V.
HannesTschofenig 0:796d0f61a05b 5 *
HannesTschofenig 0:796d0f61a05b 6 * This file is part of PolarSSL (http://www.polarssl.org)
HannesTschofenig 0:796d0f61a05b 7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
HannesTschofenig 0:796d0f61a05b 8 *
HannesTschofenig 0:796d0f61a05b 9 * All rights reserved.
HannesTschofenig 0:796d0f61a05b 10 *
HannesTschofenig 0:796d0f61a05b 11 * This program is free software; you can redistribute it and/or modify
HannesTschofenig 0:796d0f61a05b 12 * it under the terms of the GNU General Public License as published by
HannesTschofenig 0:796d0f61a05b 13 * the Free Software Foundation; either version 2 of the License, or
HannesTschofenig 0:796d0f61a05b 14 * (at your option) any later version.
HannesTschofenig 0:796d0f61a05b 15 *
HannesTschofenig 0:796d0f61a05b 16 * This program is distributed in the hope that it will be useful,
HannesTschofenig 0:796d0f61a05b 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
HannesTschofenig 0:796d0f61a05b 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
HannesTschofenig 0:796d0f61a05b 19 * GNU General Public License for more details.
HannesTschofenig 0:796d0f61a05b 20 *
HannesTschofenig 0:796d0f61a05b 21 * You should have received a copy of the GNU General Public License along
HannesTschofenig 0:796d0f61a05b 22 * with this program; if not, write to the Free Software Foundation, Inc.,
HannesTschofenig 0:796d0f61a05b 23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
HannesTschofenig 0:796d0f61a05b 24 */
HannesTschofenig 0:796d0f61a05b 25
HannesTschofenig 0:796d0f61a05b 26 #if !defined(POLARSSL_CONFIG_FILE)
HannesTschofenig 0:796d0f61a05b 27 #include "polarssl/config.h"
HannesTschofenig 0:796d0f61a05b 28 #else
HannesTschofenig 0:796d0f61a05b 29 #include POLARSSL_CONFIG_FILE
HannesTschofenig 0:796d0f61a05b 30 #endif
HannesTschofenig 0:796d0f61a05b 31
HannesTschofenig 0:796d0f61a05b 32 #if defined(POLARSSL_ASN1_WRITE_C)
HannesTschofenig 0:796d0f61a05b 33
HannesTschofenig 0:796d0f61a05b 34 #include "polarssl/asn1write.h"
HannesTschofenig 0:796d0f61a05b 35
HannesTschofenig 0:796d0f61a05b 36 #if defined(POLARSSL_PLATFORM_C)
HannesTschofenig 0:796d0f61a05b 37 #include "polarssl/platform.h"
HannesTschofenig 0:796d0f61a05b 38 #else
HannesTschofenig 0:796d0f61a05b 39 #include <stdlib.h>
HannesTschofenig 0:796d0f61a05b 40 #define polarssl_malloc malloc
HannesTschofenig 0:796d0f61a05b 41 #define polarssl_free free
HannesTschofenig 0:796d0f61a05b 42 #endif
HannesTschofenig 0:796d0f61a05b 43
HannesTschofenig 0:796d0f61a05b 44 int asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
HannesTschofenig 0:796d0f61a05b 45 {
HannesTschofenig 0:796d0f61a05b 46 if( len < 0x80 )
HannesTschofenig 0:796d0f61a05b 47 {
HannesTschofenig 0:796d0f61a05b 48 if( *p - start < 1 )
HannesTschofenig 0:796d0f61a05b 49 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
HannesTschofenig 0:796d0f61a05b 50
HannesTschofenig 0:796d0f61a05b 51 *--(*p) = (unsigned char) len;
HannesTschofenig 0:796d0f61a05b 52 return( 1 );
HannesTschofenig 0:796d0f61a05b 53 }
HannesTschofenig 0:796d0f61a05b 54
HannesTschofenig 0:796d0f61a05b 55 if( len <= 0xFF )
HannesTschofenig 0:796d0f61a05b 56 {
HannesTschofenig 0:796d0f61a05b 57 if( *p - start < 2 )
HannesTschofenig 0:796d0f61a05b 58 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
HannesTschofenig 0:796d0f61a05b 59
HannesTschofenig 0:796d0f61a05b 60 *--(*p) = (unsigned char) len;
HannesTschofenig 0:796d0f61a05b 61 *--(*p) = 0x81;
HannesTschofenig 0:796d0f61a05b 62 return( 2 );
HannesTschofenig 0:796d0f61a05b 63 }
HannesTschofenig 0:796d0f61a05b 64
HannesTschofenig 0:796d0f61a05b 65 if( *p - start < 3 )
HannesTschofenig 0:796d0f61a05b 66 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
HannesTschofenig 0:796d0f61a05b 67
HannesTschofenig 0:796d0f61a05b 68 // We assume we never have lengths larger than 65535 bytes
HannesTschofenig 0:796d0f61a05b 69 //
HannesTschofenig 0:796d0f61a05b 70 *--(*p) = len % 256;
HannesTschofenig 0:796d0f61a05b 71 *--(*p) = ( len / 256 ) % 256;
HannesTschofenig 0:796d0f61a05b 72 *--(*p) = 0x82;
HannesTschofenig 0:796d0f61a05b 73
HannesTschofenig 0:796d0f61a05b 74 return( 3 );
HannesTschofenig 0:796d0f61a05b 75 }
HannesTschofenig 0:796d0f61a05b 76
HannesTschofenig 0:796d0f61a05b 77 int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
HannesTschofenig 0:796d0f61a05b 78 {
HannesTschofenig 0:796d0f61a05b 79 if( *p - start < 1 )
HannesTschofenig 0:796d0f61a05b 80 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
HannesTschofenig 0:796d0f61a05b 81
HannesTschofenig 0:796d0f61a05b 82 *--(*p) = tag;
HannesTschofenig 0:796d0f61a05b 83
HannesTschofenig 0:796d0f61a05b 84 return( 1 );
HannesTschofenig 0:796d0f61a05b 85 }
HannesTschofenig 0:796d0f61a05b 86
HannesTschofenig 0:796d0f61a05b 87 int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 88 const unsigned char *buf, size_t size )
HannesTschofenig 0:796d0f61a05b 89 {
HannesTschofenig 0:796d0f61a05b 90 size_t len = 0;
HannesTschofenig 0:796d0f61a05b 91
HannesTschofenig 0:796d0f61a05b 92 if( *p - start < (int) size )
HannesTschofenig 0:796d0f61a05b 93 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
HannesTschofenig 0:796d0f61a05b 94
HannesTschofenig 0:796d0f61a05b 95 len = size;
HannesTschofenig 0:796d0f61a05b 96 (*p) -= len;
HannesTschofenig 0:796d0f61a05b 97 memcpy( *p, buf, len );
HannesTschofenig 0:796d0f61a05b 98
HannesTschofenig 0:796d0f61a05b 99 return( (int) len );
HannesTschofenig 0:796d0f61a05b 100 }
HannesTschofenig 0:796d0f61a05b 101
HannesTschofenig 0:796d0f61a05b 102 #if defined(POLARSSL_BIGNUM_C)
HannesTschofenig 0:796d0f61a05b 103 int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X )
HannesTschofenig 0:796d0f61a05b 104 {
HannesTschofenig 0:796d0f61a05b 105 int ret;
HannesTschofenig 0:796d0f61a05b 106 size_t len = 0;
HannesTschofenig 0:796d0f61a05b 107
HannesTschofenig 0:796d0f61a05b 108 // Write the MPI
HannesTschofenig 0:796d0f61a05b 109 //
HannesTschofenig 0:796d0f61a05b 110 len = mpi_size( X );
HannesTschofenig 0:796d0f61a05b 111
HannesTschofenig 0:796d0f61a05b 112 if( *p - start < (int) len )
HannesTschofenig 0:796d0f61a05b 113 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
HannesTschofenig 0:796d0f61a05b 114
HannesTschofenig 0:796d0f61a05b 115 (*p) -= len;
HannesTschofenig 0:796d0f61a05b 116 MPI_CHK( mpi_write_binary( X, *p, len ) );
HannesTschofenig 0:796d0f61a05b 117
HannesTschofenig 0:796d0f61a05b 118 // DER format assumes 2s complement for numbers, so the leftmost bit
HannesTschofenig 0:796d0f61a05b 119 // should be 0 for positive numbers and 1 for negative numbers.
HannesTschofenig 0:796d0f61a05b 120 //
HannesTschofenig 0:796d0f61a05b 121 if ( X->s ==1 && **p & 0x80 )
HannesTschofenig 0:796d0f61a05b 122 {
HannesTschofenig 0:796d0f61a05b 123 if( *p - start < 1 )
HannesTschofenig 0:796d0f61a05b 124 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
HannesTschofenig 0:796d0f61a05b 125
HannesTschofenig 0:796d0f61a05b 126 *--(*p) = 0x00;
HannesTschofenig 0:796d0f61a05b 127 len += 1;
HannesTschofenig 0:796d0f61a05b 128 }
HannesTschofenig 0:796d0f61a05b 129
HannesTschofenig 0:796d0f61a05b 130 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
HannesTschofenig 0:796d0f61a05b 131 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
HannesTschofenig 0:796d0f61a05b 132
HannesTschofenig 0:796d0f61a05b 133 ret = (int) len;
HannesTschofenig 0:796d0f61a05b 134
HannesTschofenig 0:796d0f61a05b 135 cleanup:
HannesTschofenig 0:796d0f61a05b 136 return( ret );
HannesTschofenig 0:796d0f61a05b 137 }
HannesTschofenig 0:796d0f61a05b 138 #endif /* POLARSSL_BIGNUM_C */
HannesTschofenig 0:796d0f61a05b 139
HannesTschofenig 0:796d0f61a05b 140 int asn1_write_null( unsigned char **p, unsigned char *start )
HannesTschofenig 0:796d0f61a05b 141 {
HannesTschofenig 0:796d0f61a05b 142 int ret;
HannesTschofenig 0:796d0f61a05b 143 size_t len = 0;
HannesTschofenig 0:796d0f61a05b 144
HannesTschofenig 0:796d0f61a05b 145 // Write NULL
HannesTschofenig 0:796d0f61a05b 146 //
HannesTschofenig 0:796d0f61a05b 147 ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
HannesTschofenig 0:796d0f61a05b 148 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
HannesTschofenig 0:796d0f61a05b 149
HannesTschofenig 0:796d0f61a05b 150 return( (int) len );
HannesTschofenig 0:796d0f61a05b 151 }
HannesTschofenig 0:796d0f61a05b 152
HannesTschofenig 0:796d0f61a05b 153 int asn1_write_oid( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 154 const char *oid, size_t oid_len )
HannesTschofenig 0:796d0f61a05b 155 {
HannesTschofenig 0:796d0f61a05b 156 int ret;
HannesTschofenig 0:796d0f61a05b 157 size_t len = 0;
HannesTschofenig 0:796d0f61a05b 158
HannesTschofenig 0:796d0f61a05b 159 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
HannesTschofenig 0:796d0f61a05b 160 (const unsigned char *) oid, oid_len ) );
HannesTschofenig 0:796d0f61a05b 161 ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
HannesTschofenig 0:796d0f61a05b 162 ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
HannesTschofenig 0:796d0f61a05b 163
HannesTschofenig 0:796d0f61a05b 164 return( (int) len );
HannesTschofenig 0:796d0f61a05b 165 }
HannesTschofenig 0:796d0f61a05b 166
HannesTschofenig 0:796d0f61a05b 167 int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 168 const char *oid, size_t oid_len,
HannesTschofenig 0:796d0f61a05b 169 size_t par_len )
HannesTschofenig 0:796d0f61a05b 170 {
HannesTschofenig 0:796d0f61a05b 171 int ret;
HannesTschofenig 0:796d0f61a05b 172 size_t len = 0;
HannesTschofenig 0:796d0f61a05b 173
HannesTschofenig 0:796d0f61a05b 174 if( par_len == 0 )
HannesTschofenig 0:796d0f61a05b 175 ASN1_CHK_ADD( len, asn1_write_null( p, start ) );
HannesTschofenig 0:796d0f61a05b 176 else
HannesTschofenig 0:796d0f61a05b 177 len += par_len;
HannesTschofenig 0:796d0f61a05b 178
HannesTschofenig 0:796d0f61a05b 179 ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
HannesTschofenig 0:796d0f61a05b 180
HannesTschofenig 0:796d0f61a05b 181 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
HannesTschofenig 0:796d0f61a05b 182 ASN1_CHK_ADD( len, asn1_write_tag( p, start,
HannesTschofenig 0:796d0f61a05b 183 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
HannesTschofenig 0:796d0f61a05b 184
HannesTschofenig 0:796d0f61a05b 185 return( (int) len );
HannesTschofenig 0:796d0f61a05b 186 }
HannesTschofenig 0:796d0f61a05b 187
HannesTschofenig 0:796d0f61a05b 188 int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
HannesTschofenig 0:796d0f61a05b 189 {
HannesTschofenig 0:796d0f61a05b 190 int ret;
HannesTschofenig 0:796d0f61a05b 191 size_t len = 0;
HannesTschofenig 0:796d0f61a05b 192
HannesTschofenig 0:796d0f61a05b 193 if( *p - start < 1 )
HannesTschofenig 0:796d0f61a05b 194 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
HannesTschofenig 0:796d0f61a05b 195
HannesTschofenig 0:796d0f61a05b 196 *--(*p) = (boolean) ? 1 : 0;
HannesTschofenig 0:796d0f61a05b 197 len++;
HannesTschofenig 0:796d0f61a05b 198
HannesTschofenig 0:796d0f61a05b 199 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
HannesTschofenig 0:796d0f61a05b 200 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BOOLEAN ) );
HannesTschofenig 0:796d0f61a05b 201
HannesTschofenig 0:796d0f61a05b 202 return( (int) len );
HannesTschofenig 0:796d0f61a05b 203 }
HannesTschofenig 0:796d0f61a05b 204
HannesTschofenig 0:796d0f61a05b 205 int asn1_write_int( unsigned char **p, unsigned char *start, int val )
HannesTschofenig 0:796d0f61a05b 206 {
HannesTschofenig 0:796d0f61a05b 207 int ret;
HannesTschofenig 0:796d0f61a05b 208 size_t len = 0;
HannesTschofenig 0:796d0f61a05b 209
HannesTschofenig 0:796d0f61a05b 210 // TODO negative values and values larger than 128
HannesTschofenig 0:796d0f61a05b 211 // DER format assumes 2s complement for numbers, so the leftmost bit
HannesTschofenig 0:796d0f61a05b 212 // should be 0 for positive numbers and 1 for negative numbers.
HannesTschofenig 0:796d0f61a05b 213 //
HannesTschofenig 0:796d0f61a05b 214 if( *p - start < 1 )
HannesTschofenig 0:796d0f61a05b 215 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
HannesTschofenig 0:796d0f61a05b 216
HannesTschofenig 0:796d0f61a05b 217 len += 1;
HannesTschofenig 0:796d0f61a05b 218 *--(*p) = val;
HannesTschofenig 0:796d0f61a05b 219
HannesTschofenig 0:796d0f61a05b 220 if ( val > 0 && **p & 0x80 )
HannesTschofenig 0:796d0f61a05b 221 {
HannesTschofenig 0:796d0f61a05b 222 if( *p - start < 1 )
HannesTschofenig 0:796d0f61a05b 223 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
HannesTschofenig 0:796d0f61a05b 224
HannesTschofenig 0:796d0f61a05b 225 *--(*p) = 0x00;
HannesTschofenig 0:796d0f61a05b 226 len += 1;
HannesTschofenig 0:796d0f61a05b 227 }
HannesTschofenig 0:796d0f61a05b 228
HannesTschofenig 0:796d0f61a05b 229 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
HannesTschofenig 0:796d0f61a05b 230 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
HannesTschofenig 0:796d0f61a05b 231
HannesTschofenig 0:796d0f61a05b 232 return( (int) len );
HannesTschofenig 0:796d0f61a05b 233 }
HannesTschofenig 0:796d0f61a05b 234
HannesTschofenig 0:796d0f61a05b 235 int asn1_write_printable_string( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 236 const char *text, size_t text_len )
HannesTschofenig 0:796d0f61a05b 237 {
HannesTschofenig 0:796d0f61a05b 238 int ret;
HannesTschofenig 0:796d0f61a05b 239 size_t len = 0;
HannesTschofenig 0:796d0f61a05b 240
HannesTschofenig 0:796d0f61a05b 241 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
HannesTschofenig 0:796d0f61a05b 242 (const unsigned char *) text, text_len ) );
HannesTschofenig 0:796d0f61a05b 243
HannesTschofenig 0:796d0f61a05b 244 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
HannesTschofenig 0:796d0f61a05b 245 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
HannesTschofenig 0:796d0f61a05b 246
HannesTschofenig 0:796d0f61a05b 247 return( (int) len );
HannesTschofenig 0:796d0f61a05b 248 }
HannesTschofenig 0:796d0f61a05b 249
HannesTschofenig 0:796d0f61a05b 250 int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 251 const char *text, size_t text_len )
HannesTschofenig 0:796d0f61a05b 252 {
HannesTschofenig 0:796d0f61a05b 253 int ret;
HannesTschofenig 0:796d0f61a05b 254 size_t len = 0;
HannesTschofenig 0:796d0f61a05b 255
HannesTschofenig 0:796d0f61a05b 256 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
HannesTschofenig 0:796d0f61a05b 257 (const unsigned char *) text, text_len ) );
HannesTschofenig 0:796d0f61a05b 258
HannesTschofenig 0:796d0f61a05b 259 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
HannesTschofenig 0:796d0f61a05b 260 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
HannesTschofenig 0:796d0f61a05b 261
HannesTschofenig 0:796d0f61a05b 262 return( (int) len );
HannesTschofenig 0:796d0f61a05b 263 }
HannesTschofenig 0:796d0f61a05b 264
HannesTschofenig 0:796d0f61a05b 265 int asn1_write_bitstring( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 266 const unsigned char *buf, size_t bits )
HannesTschofenig 0:796d0f61a05b 267 {
HannesTschofenig 0:796d0f61a05b 268 int ret;
HannesTschofenig 0:796d0f61a05b 269 size_t len = 0, size;
HannesTschofenig 0:796d0f61a05b 270
HannesTschofenig 0:796d0f61a05b 271 size = ( bits / 8 ) + ( ( bits % 8 ) ? 1 : 0 );
HannesTschofenig 0:796d0f61a05b 272
HannesTschofenig 0:796d0f61a05b 273 // Calculate byte length
HannesTschofenig 0:796d0f61a05b 274 //
HannesTschofenig 0:796d0f61a05b 275 if( *p - start < (int) size + 1 )
HannesTschofenig 0:796d0f61a05b 276 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
HannesTschofenig 0:796d0f61a05b 277
HannesTschofenig 0:796d0f61a05b 278 len = size + 1;
HannesTschofenig 0:796d0f61a05b 279 (*p) -= size;
HannesTschofenig 0:796d0f61a05b 280 memcpy( *p, buf, size );
HannesTschofenig 0:796d0f61a05b 281
HannesTschofenig 0:796d0f61a05b 282 // Write unused bits
HannesTschofenig 0:796d0f61a05b 283 //
HannesTschofenig 0:796d0f61a05b 284 *--(*p) = (unsigned char) (size * 8 - bits);
HannesTschofenig 0:796d0f61a05b 285
HannesTschofenig 0:796d0f61a05b 286 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
HannesTschofenig 0:796d0f61a05b 287 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
HannesTschofenig 0:796d0f61a05b 288
HannesTschofenig 0:796d0f61a05b 289 return( (int) len );
HannesTschofenig 0:796d0f61a05b 290 }
HannesTschofenig 0:796d0f61a05b 291
HannesTschofenig 0:796d0f61a05b 292 int asn1_write_octet_string( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 293 const unsigned char *buf, size_t size )
HannesTschofenig 0:796d0f61a05b 294 {
HannesTschofenig 0:796d0f61a05b 295 int ret;
HannesTschofenig 0:796d0f61a05b 296 size_t len = 0;
HannesTschofenig 0:796d0f61a05b 297
HannesTschofenig 0:796d0f61a05b 298 ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, buf, size ) );
HannesTschofenig 0:796d0f61a05b 299
HannesTschofenig 0:796d0f61a05b 300 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
HannesTschofenig 0:796d0f61a05b 301 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
HannesTschofenig 0:796d0f61a05b 302
HannesTschofenig 0:796d0f61a05b 303 return( (int) len );
HannesTschofenig 0:796d0f61a05b 304 }
HannesTschofenig 0:796d0f61a05b 305
HannesTschofenig 0:796d0f61a05b 306 asn1_named_data *asn1_store_named_data( asn1_named_data **head,
HannesTschofenig 0:796d0f61a05b 307 const char *oid, size_t oid_len,
HannesTschofenig 0:796d0f61a05b 308 const unsigned char *val,
HannesTschofenig 0:796d0f61a05b 309 size_t val_len )
HannesTschofenig 0:796d0f61a05b 310 {
HannesTschofenig 0:796d0f61a05b 311 asn1_named_data *cur;
HannesTschofenig 0:796d0f61a05b 312
HannesTschofenig 0:796d0f61a05b 313 if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
HannesTschofenig 0:796d0f61a05b 314 {
HannesTschofenig 0:796d0f61a05b 315 // Add new entry if not present yet based on OID
HannesTschofenig 0:796d0f61a05b 316 //
HannesTschofenig 0:796d0f61a05b 317 if( ( cur = polarssl_malloc( sizeof(asn1_named_data) ) ) == NULL )
HannesTschofenig 0:796d0f61a05b 318 return( NULL );
HannesTschofenig 0:796d0f61a05b 319
HannesTschofenig 0:796d0f61a05b 320 memset( cur, 0, sizeof(asn1_named_data) );
HannesTschofenig 0:796d0f61a05b 321
HannesTschofenig 0:796d0f61a05b 322 cur->oid.len = oid_len;
HannesTschofenig 0:796d0f61a05b 323 cur->oid.p = polarssl_malloc( oid_len );
HannesTschofenig 0:796d0f61a05b 324 if( cur->oid.p == NULL )
HannesTschofenig 0:796d0f61a05b 325 {
HannesTschofenig 0:796d0f61a05b 326 polarssl_free( cur );
HannesTschofenig 0:796d0f61a05b 327 return( NULL );
HannesTschofenig 0:796d0f61a05b 328 }
HannesTschofenig 0:796d0f61a05b 329
HannesTschofenig 0:796d0f61a05b 330 cur->val.len = val_len;
HannesTschofenig 0:796d0f61a05b 331 cur->val.p = polarssl_malloc( val_len );
HannesTschofenig 0:796d0f61a05b 332 if( cur->val.p == NULL )
HannesTschofenig 0:796d0f61a05b 333 {
HannesTschofenig 0:796d0f61a05b 334 polarssl_free( cur->oid.p );
HannesTschofenig 0:796d0f61a05b 335 polarssl_free( cur );
HannesTschofenig 0:796d0f61a05b 336 return( NULL );
HannesTschofenig 0:796d0f61a05b 337 }
HannesTschofenig 0:796d0f61a05b 338
HannesTschofenig 0:796d0f61a05b 339 memcpy( cur->oid.p, oid, oid_len );
HannesTschofenig 0:796d0f61a05b 340
HannesTschofenig 0:796d0f61a05b 341 cur->next = *head;
HannesTschofenig 0:796d0f61a05b 342 *head = cur;
HannesTschofenig 0:796d0f61a05b 343 }
HannesTschofenig 0:796d0f61a05b 344 else if( cur->val.len < val_len )
HannesTschofenig 0:796d0f61a05b 345 {
HannesTschofenig 0:796d0f61a05b 346 // Enlarge existing value buffer if needed
HannesTschofenig 0:796d0f61a05b 347 //
HannesTschofenig 0:796d0f61a05b 348 polarssl_free( cur->val.p );
HannesTschofenig 0:796d0f61a05b 349 cur->val.p = NULL;
HannesTschofenig 0:796d0f61a05b 350
HannesTschofenig 0:796d0f61a05b 351 cur->val.len = val_len;
HannesTschofenig 0:796d0f61a05b 352 cur->val.p = polarssl_malloc( val_len );
HannesTschofenig 0:796d0f61a05b 353 if( cur->val.p == NULL )
HannesTschofenig 0:796d0f61a05b 354 {
HannesTschofenig 0:796d0f61a05b 355 polarssl_free( cur->oid.p );
HannesTschofenig 0:796d0f61a05b 356 polarssl_free( cur );
HannesTschofenig 0:796d0f61a05b 357 return( NULL );
HannesTschofenig 0:796d0f61a05b 358 }
HannesTschofenig 0:796d0f61a05b 359 }
HannesTschofenig 0:796d0f61a05b 360
HannesTschofenig 0:796d0f61a05b 361 if( val != NULL )
HannesTschofenig 0:796d0f61a05b 362 memcpy( cur->val.p, val, val_len );
HannesTschofenig 0:796d0f61a05b 363
HannesTschofenig 0:796d0f61a05b 364 return( cur );
HannesTschofenig 0:796d0f61a05b 365 }
HannesTschofenig 0:796d0f61a05b 366 #endif /* POLARSSL_ASN1_WRITE_C */
HannesTschofenig 0:796d0f61a05b 367
HannesTschofenig 0:796d0f61a05b 368