Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ASN.1 buffer writing functionality |
| 3 | * |
| 4 | * Copyright (C) 2006-2012, Brainspark B.V. |
| 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
| 26 | #include "polarssl/config.h" |
| 27 | |
| 28 | #if defined(POLARSSL_ASN1_WRITE_C) |
| 29 | |
| 30 | #include "polarssl/asn1write.h" |
| 31 | |
| 32 | int asn1_write_len( unsigned char **p, unsigned char *start, size_t len ) |
| 33 | { |
| 34 | if( len < 0x80 ) |
| 35 | { |
| 36 | if( *p - start < 1 ) |
| 37 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 38 | |
| 39 | *--(*p) = len; |
| 40 | return( 1 ); |
| 41 | } |
| 42 | |
| 43 | if( len <= 0xFF ) |
| 44 | { |
| 45 | if( *p - start < 2 ) |
| 46 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 47 | |
| 48 | *--(*p) = len; |
| 49 | *--(*p) = 0x81; |
| 50 | return( 2 ); |
| 51 | } |
| 52 | |
| 53 | if( *p - start < 3 ) |
| 54 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 55 | |
| 56 | // We assume we never have lengths larger than 65535 bytes |
| 57 | // |
| 58 | *--(*p) = len % 256; |
| 59 | *--(*p) = ( len / 256 ) % 256; |
| 60 | *--(*p) = 0x82; |
| 61 | |
| 62 | return( 3 ); |
| 63 | } |
| 64 | |
| 65 | int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag ) |
| 66 | { |
| 67 | if( *p - start < 1 ) |
| 68 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 69 | |
| 70 | *--(*p) = tag; |
| 71 | |
| 72 | return( 1 ); |
| 73 | } |
| 74 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame^] | 75 | #if defined(POLARSSL_BIGNUM_C) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 76 | int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X ) |
| 77 | { |
| 78 | int ret; |
| 79 | size_t len = 0; |
| 80 | |
| 81 | // Write the MPI |
| 82 | // |
| 83 | len = mpi_size( X ); |
| 84 | |
| 85 | if( *p - start < (int) len ) |
| 86 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 87 | |
| 88 | (*p) -= len; |
| 89 | mpi_write_binary( X, *p, len ); |
| 90 | |
| 91 | // DER format assumes 2s complement for numbers, so the leftmost bit |
| 92 | // should be 0 for positive numbers and 1 for negative numbers. |
| 93 | // |
| 94 | if ( X->s ==1 && **p & 0x80 ) |
| 95 | { |
| 96 | if( *p - start < 1 ) |
| 97 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 98 | |
| 99 | *--(*p) = 0x00; |
| 100 | len += 1; |
| 101 | } |
| 102 | |
| 103 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 104 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) ); |
| 105 | |
| 106 | return( len ); |
| 107 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame^] | 108 | #endif /* POLARSSL_BIGNUM_C */ |
| 109 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 110 | int asn1_write_null( unsigned char **p, unsigned char *start ) |
| 111 | { |
| 112 | int ret; |
| 113 | size_t len = 0; |
| 114 | |
| 115 | // Write NULL |
| 116 | // |
| 117 | ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) ); |
| 118 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) ); |
| 119 | |
| 120 | return( len ); |
| 121 | } |
| 122 | |
Paul Bakker | 37de6be | 2013-04-07 13:11:31 +0200 | [diff] [blame] | 123 | int asn1_write_oid( unsigned char **p, unsigned char *start, const char *oid ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 124 | { |
| 125 | int ret; |
| 126 | size_t len = 0; |
| 127 | |
| 128 | // Write OID |
| 129 | // |
| 130 | len = strlen( oid ); |
| 131 | |
| 132 | if( *p - start < (int) len ) |
| 133 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 134 | |
| 135 | (*p) -= len; |
| 136 | memcpy( *p, oid, len ); |
| 137 | |
| 138 | ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) ); |
| 139 | ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) ); |
| 140 | |
| 141 | return( len ); |
| 142 | } |
| 143 | |
| 144 | int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start, |
Paul Bakker | 37de6be | 2013-04-07 13:11:31 +0200 | [diff] [blame] | 145 | const char *algorithm_oid ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 146 | { |
| 147 | int ret; |
| 148 | size_t null_len = 0; |
| 149 | size_t oid_len = 0; |
| 150 | size_t len = 0; |
| 151 | |
| 152 | // Write NULL |
| 153 | // |
| 154 | ASN1_CHK_ADD( null_len, asn1_write_null( p, start ) ); |
| 155 | |
| 156 | // Write OID |
| 157 | // |
| 158 | ASN1_CHK_ADD( oid_len, asn1_write_oid( p, start, algorithm_oid ) ); |
| 159 | |
| 160 | len = oid_len + null_len; |
| 161 | ASN1_CHK_ADD( len, asn1_write_len( p, start, oid_len + null_len ) ); |
| 162 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, |
| 163 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 164 | |
| 165 | return( len ); |
| 166 | } |
| 167 | |
| 168 | int asn1_write_int( unsigned char **p, unsigned char *start, int val ) |
| 169 | { |
| 170 | int ret; |
| 171 | size_t len = 0; |
| 172 | |
| 173 | // TODO negative values and values larger than 128 |
| 174 | // DER format assumes 2s complement for numbers, so the leftmost bit |
| 175 | // should be 0 for positive numbers and 1 for negative numbers. |
| 176 | // |
| 177 | if( *p - start < 1 ) |
| 178 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 179 | |
| 180 | len += 1; |
| 181 | *--(*p) = val; |
| 182 | |
| 183 | if ( val > 0 && **p & 0x80 ) |
| 184 | { |
| 185 | if( *p - start < 1 ) |
| 186 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 187 | |
| 188 | *--(*p) = 0x00; |
| 189 | len += 1; |
| 190 | } |
| 191 | |
| 192 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 193 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) ); |
| 194 | |
| 195 | return( len ); |
| 196 | } |
| 197 | |
| 198 | int asn1_write_printable_string( unsigned char **p, unsigned char *start, |
| 199 | char *text ) |
| 200 | { |
| 201 | int ret; |
| 202 | size_t len = 0; |
| 203 | |
| 204 | // Write string |
| 205 | // |
| 206 | len = strlen( text ); |
| 207 | |
| 208 | if( *p - start < (int) len ) |
| 209 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 210 | |
| 211 | (*p) -= len; |
| 212 | memcpy( *p, text, len ); |
| 213 | |
| 214 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 215 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) ); |
| 216 | |
| 217 | return( len ); |
| 218 | } |
| 219 | |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 220 | int asn1_write_ia5_string( unsigned char **p, unsigned char *start, |
| 221 | char *text ) |
| 222 | { |
| 223 | int ret; |
| 224 | size_t len = 0; |
| 225 | |
| 226 | // Write string |
| 227 | // |
| 228 | len = strlen( text ); |
| 229 | |
| 230 | if( *p - start < (int) len ) |
| 231 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 232 | |
| 233 | (*p) -= len; |
| 234 | memcpy( *p, text, len ); |
| 235 | |
| 236 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 237 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) ); |
| 238 | |
| 239 | return( len ); |
| 240 | } |
| 241 | |
| 242 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 243 | #endif |