Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ASN.1 buffer writing functionality |
| 3 | * |
Bence Szépkúti | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 45 | */ |
| 46 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 48 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 49 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 51 | #endif |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 52 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 55 | #include "mbedtls/asn1write.h" |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 56 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 57 | #include <string.h> |
| 58 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 60 | #include "mbedtls/platform.h" |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 61 | #else |
| 62 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 63 | #define mbedtls_calloc calloc |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | #define mbedtls_free free |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 65 | #endif |
| 66 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | 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] | 68 | { |
| 69 | if( len < 0x80 ) |
| 70 | { |
| 71 | if( *p - start < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 73 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 74 | *--(*p) = (unsigned char) len; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 75 | return( 1 ); |
| 76 | } |
| 77 | |
| 78 | if( len <= 0xFF ) |
| 79 | { |
| 80 | if( *p - start < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 82 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 83 | *--(*p) = (unsigned char) len; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 84 | *--(*p) = 0x81; |
| 85 | return( 2 ); |
| 86 | } |
| 87 | |
Paul Bakker | c7d6bd4 | 2016-07-14 11:39:56 +0100 | [diff] [blame] | 88 | if( len <= 0xFFFF ) |
| 89 | { |
| 90 | if( *p - start < 3 ) |
| 91 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 92 | |
Paul Bakker | c7d6bd4 | 2016-07-14 11:39:56 +0100 | [diff] [blame] | 93 | *--(*p) = ( len ) & 0xFF; |
| 94 | *--(*p) = ( len >> 8 ) & 0xFF; |
| 95 | *--(*p) = 0x82; |
| 96 | return( 3 ); |
| 97 | } |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 98 | |
Paul Bakker | c7d6bd4 | 2016-07-14 11:39:56 +0100 | [diff] [blame] | 99 | if( len <= 0xFFFFFF ) |
| 100 | { |
| 101 | if( *p - start < 4 ) |
| 102 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 103 | |
| 104 | *--(*p) = ( len ) & 0xFF; |
| 105 | *--(*p) = ( len >> 8 ) & 0xFF; |
| 106 | *--(*p) = ( len >> 16 ) & 0xFF; |
| 107 | *--(*p) = 0x83; |
| 108 | return( 4 ); |
| 109 | } |
| 110 | |
Azim Khan | 45b79cf | 2018-05-23 16:55:16 +0100 | [diff] [blame] | 111 | #if SIZE_MAX > 0xFFFFFFFF |
Paul Bakker | c7d6bd4 | 2016-07-14 11:39:56 +0100 | [diff] [blame] | 112 | if( len <= 0xFFFFFFFF ) |
Azim Khan | 45b79cf | 2018-05-23 16:55:16 +0100 | [diff] [blame] | 113 | #endif |
Paul Bakker | c7d6bd4 | 2016-07-14 11:39:56 +0100 | [diff] [blame] | 114 | { |
| 115 | if( *p - start < 5 ) |
| 116 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 117 | |
| 118 | *--(*p) = ( len ) & 0xFF; |
| 119 | *--(*p) = ( len >> 8 ) & 0xFF; |
| 120 | *--(*p) = ( len >> 16 ) & 0xFF; |
| 121 | *--(*p) = ( len >> 24 ) & 0xFF; |
| 122 | *--(*p) = 0x84; |
| 123 | return( 5 ); |
| 124 | } |
| 125 | |
Azim Khan | 45b79cf | 2018-05-23 16:55:16 +0100 | [diff] [blame] | 126 | #if SIZE_MAX > 0xFFFFFFFF |
Paul Bakker | c7d6bd4 | 2016-07-14 11:39:56 +0100 | [diff] [blame] | 127 | return( MBEDTLS_ERR_ASN1_INVALID_LENGTH ); |
Azim Khan | 45b79cf | 2018-05-23 16:55:16 +0100 | [diff] [blame] | 128 | #endif |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | 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] | 132 | { |
| 133 | if( *p - start < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 135 | |
| 136 | *--(*p) = tag; |
| 137 | |
| 138 | return( 1 ); |
| 139 | } |
| 140 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start, |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 142 | const unsigned char *buf, size_t size ) |
| 143 | { |
| 144 | size_t len = 0; |
| 145 | |
Manuel Pégourié-Gonnard | 22c3b7b | 2015-10-21 12:07:47 +0200 | [diff] [blame] | 146 | if( *p < start || (size_t)( *p - start ) < size ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 148 | |
| 149 | len = size; |
| 150 | (*p) -= len; |
| 151 | memcpy( *p, buf, len ); |
| 152 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 153 | return( (int) len ); |
Paul Bakker | 9852d00 | 2013-08-26 17:56:37 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | #if defined(MBEDTLS_BIGNUM_C) |
| 157 | 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] | 158 | { |
| 159 | int ret; |
| 160 | size_t len = 0; |
| 161 | |
| 162 | // Write the MPI |
| 163 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | len = mbedtls_mpi_size( X ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 165 | |
Manuel Pégourié-Gonnard | 22c3b7b | 2015-10-21 12:07:47 +0200 | [diff] [blame] | 166 | if( *p < start || (size_t)( *p - start ) < len ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 168 | |
| 169 | (*p) -= len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 171 | |
| 172 | // DER format assumes 2s complement for numbers, so the leftmost bit |
| 173 | // should be 0 for positive numbers and 1 for negative numbers. |
| 174 | // |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 175 | if( X->s ==1 && **p & 0x80 ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 176 | { |
| 177 | if( *p - start < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 179 | |
| 180 | *--(*p) = 0x00; |
| 181 | len += 1; |
| 182 | } |
| 183 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 185 | 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] | 186 | |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 187 | ret = (int) len; |
| 188 | |
| 189 | cleanup: |
| 190 | return( ret ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 191 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | #endif /* MBEDTLS_BIGNUM_C */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 193 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 195 | { |
| 196 | int ret; |
| 197 | size_t len = 0; |
| 198 | |
| 199 | // Write NULL |
| 200 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) ); |
| 202 | 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] | 203 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 204 | return( (int) len ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 207 | int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 208 | const char *oid, size_t oid_len ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 209 | { |
| 210 | int ret; |
| 211 | size_t len = 0; |
| 212 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 213 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 214 | (const unsigned char *) oid, oid_len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) ); |
| 216 | 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] | 217 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 218 | return( (int) len ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 221 | 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] | 222 | const char *oid, size_t oid_len, |
| 223 | size_t par_len ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 224 | { |
| 225 | int ret; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 226 | size_t len = 0; |
| 227 | |
Manuel Pégourié-Gonnard | edda904 | 2013-09-12 02:17:54 +0200 | [diff] [blame] | 228 | if( par_len == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | 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] | 230 | else |
| 231 | len += par_len; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 232 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | 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] | 234 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 235 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 236 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 237 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 238 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 239 | return( (int) len ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | 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] | 243 | { |
| 244 | int ret; |
| 245 | size_t len = 0; |
| 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 | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 249 | |
Jonathan Leroy | 87c96c2 | 2015-10-14 09:41:56 +0200 | [diff] [blame] | 250 | *--(*p) = (boolean) ? 255 : 0; |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 251 | len++; |
| 252 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 254 | 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] | 255 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 256 | return( (int) len ); |
Paul Bakker | 329def3 | 2013-09-06 16:34:38 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | 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] | 260 | { |
| 261 | int ret; |
| 262 | size_t len = 0; |
| 263 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 264 | if( *p - start < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 266 | |
| 267 | len += 1; |
| 268 | *--(*p) = val; |
| 269 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 270 | if( val > 0 && **p & 0x80 ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 271 | { |
| 272 | if( *p - start < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 273 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 274 | |
| 275 | *--(*p) = 0x00; |
| 276 | len += 1; |
| 277 | } |
| 278 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 280 | 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] | 281 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 282 | return( (int) len ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 283 | } |
| 284 | |
thomas-dee | eba6c9b | 2018-09-19 09:10:37 +0200 | [diff] [blame] | 285 | int mbedtls_asn1_write_tagged_string( unsigned char **p, unsigned char *start, int tag, |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 286 | const char *text, size_t text_len ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 287 | { |
| 288 | int ret; |
| 289 | size_t len = 0; |
| 290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 291 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 292 | (const unsigned char *) text, text_len ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 293 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 294 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 295 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, tag ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 296 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 297 | return( (int) len ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 298 | } |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 299 | |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 300 | int mbedtls_asn1_write_utf8_string( unsigned char **p, unsigned char *start, |
| 301 | const char *text, size_t text_len ) |
| 302 | { |
thomas-dee | eba6c9b | 2018-09-19 09:10:37 +0200 | [diff] [blame] | 303 | return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len) ); |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start, |
| 307 | const char *text, size_t text_len ) |
| 308 | { |
thomas-dee | eba6c9b | 2018-09-19 09:10:37 +0200 | [diff] [blame] | 309 | return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text, text_len) ); |
Jaeden Amero | 23f954d | 2018-05-17 11:46:13 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 312 | int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start, |
Paul Bakker | 5f45e62 | 2013-09-09 12:02:36 +0200 | [diff] [blame] | 313 | const char *text, size_t text_len ) |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 314 | { |
thomas-dee | eba6c9b | 2018-09-19 09:10:37 +0200 | [diff] [blame] | 315 | return( mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len) ); |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 316 | } |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 317 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 318 | int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start, |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 319 | const unsigned char *buf, size_t bits ) |
| 320 | { |
| 321 | int ret; |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 322 | size_t len = 0; |
| 323 | size_t unused_bits, byte_len; |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 324 | |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 325 | byte_len = ( bits + 7 ) / 8; |
| 326 | unused_bits = ( byte_len * 8 ) - bits; |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 327 | |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 328 | if( *p < start || (size_t)( *p - start ) < byte_len + 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 329 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 330 | |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 331 | len = byte_len + 1; |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 332 | |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 333 | /* Write the bitstring. Ensure the unused bits are zeroed */ |
| 334 | if( byte_len > 0 ) |
| 335 | { |
| 336 | byte_len--; |
| 337 | *--( *p ) = buf[byte_len] & ~( ( 0x1 << unused_bits ) - 1 ); |
| 338 | ( *p ) -= byte_len; |
| 339 | memcpy( *p, buf, byte_len ); |
| 340 | } |
| 341 | |
| 342 | /* Write unused bits */ |
| 343 | *--( *p ) = (unsigned char)unused_bits; |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 344 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 345 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 346 | 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] | 347 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 348 | return( (int) len ); |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 351 | int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start, |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 352 | const unsigned char *buf, size_t size ) |
| 353 | { |
| 354 | int ret; |
| 355 | size_t len = 0; |
| 356 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 357 | 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] | 358 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 359 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 360 | 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] | 361 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 362 | return( (int) len ); |
Paul Bakker | 598e450 | 2013-08-25 14:46:39 +0200 | [diff] [blame] | 363 | } |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 364 | |
Hanno Becker | 44da18a | 2018-10-12 10:42:13 +0100 | [diff] [blame] | 365 | |
| 366 | /* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(), |
| 367 | * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */ |
| 368 | static mbedtls_asn1_named_data *asn1_find_named_data( |
| 369 | mbedtls_asn1_named_data *list, |
| 370 | const char *oid, size_t len ) |
| 371 | { |
| 372 | while( list != NULL ) |
| 373 | { |
| 374 | if( list->oid.len == len && |
| 375 | memcmp( list->oid.p, oid, len ) == 0 ) |
| 376 | { |
| 377 | break; |
| 378 | } |
| 379 | |
| 380 | list = list->next; |
| 381 | } |
| 382 | |
| 383 | return( list ); |
| 384 | } |
| 385 | |
| 386 | mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( |
| 387 | mbedtls_asn1_named_data **head, |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 388 | const char *oid, size_t oid_len, |
| 389 | const unsigned char *val, |
| 390 | size_t val_len ) |
| 391 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 392 | mbedtls_asn1_named_data *cur; |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 393 | |
Hanno Becker | 44da18a | 2018-10-12 10:42:13 +0100 | [diff] [blame] | 394 | if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL ) |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 395 | { |
| 396 | // Add new entry if not present yet based on OID |
| 397 | // |
Simon Butcher | 2917689 | 2016-05-20 00:19:09 +0100 | [diff] [blame] | 398 | cur = (mbedtls_asn1_named_data*)mbedtls_calloc( 1, |
| 399 | sizeof(mbedtls_asn1_named_data) ); |
| 400 | if( cur == NULL ) |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 401 | return( NULL ); |
| 402 | |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 403 | cur->oid.len = oid_len; |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 404 | cur->oid.p = mbedtls_calloc( 1, oid_len ); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 405 | if( cur->oid.p == NULL ) |
| 406 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 407 | mbedtls_free( cur ); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 408 | return( NULL ); |
| 409 | } |
| 410 | |
Manuel Pégourié-Gonnard | e5b0fc1 | 2014-11-12 22:27:42 +0100 | [diff] [blame] | 411 | memcpy( cur->oid.p, oid, oid_len ); |
| 412 | |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 413 | cur->val.len = val_len; |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 414 | cur->val.p = mbedtls_calloc( 1, val_len ); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 415 | if( cur->val.p == NULL ) |
| 416 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 417 | mbedtls_free( cur->oid.p ); |
| 418 | mbedtls_free( cur ); |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 419 | return( NULL ); |
| 420 | } |
| 421 | |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 422 | cur->next = *head; |
| 423 | *head = cur; |
| 424 | } |
| 425 | else if( cur->val.len < val_len ) |
| 426 | { |
Manuel Pégourié-Gonnard | 97b5209 | 2015-12-10 10:50:51 +0100 | [diff] [blame] | 427 | /* |
| 428 | * Enlarge existing value buffer if needed |
| 429 | * Preserve old data until the allocation succeeded, to leave list in |
| 430 | * a consistent state in case allocation fails. |
| 431 | */ |
| 432 | void *p = mbedtls_calloc( 1, val_len ); |
| 433 | if( p == NULL ) |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 434 | return( NULL ); |
Manuel Pégourié-Gonnard | 97b5209 | 2015-12-10 10:50:51 +0100 | [diff] [blame] | 435 | |
| 436 | mbedtls_free( cur->val.p ); |
| 437 | cur->val.p = p; |
| 438 | cur->val.len = val_len; |
Paul Bakker | 59ba59f | 2013-09-09 11:26:00 +0200 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | if( val != NULL ) |
| 442 | memcpy( cur->val.p, val, val_len ); |
| 443 | |
| 444 | return( cur ); |
| 445 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 446 | #endif /* MBEDTLS_ASN1_WRITE_C */ |