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