Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * X509 buffer writing functionality |
| 3 | * |
Paul Bakker | 530927b | 2015-02-13 14:24:10 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | e12abf9 | 2015-01-28 17:13:45 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #include "polarssl/config.h" |
| 24 | |
| 25 | #if defined(POLARSSL_X509_WRITE_C) |
| 26 | |
| 27 | #include "polarssl/asn1write.h" |
| 28 | #include "polarssl/x509write.h" |
| 29 | #include "polarssl/x509.h" |
| 30 | #include "polarssl/sha1.h" |
Paul Bakker | 3cac5e0 | 2012-02-16 14:08:06 +0000 | [diff] [blame] | 31 | #include "polarssl/sha2.h" |
| 32 | #include "polarssl/sha4.h" |
| 33 | #include "polarssl/md4.h" |
| 34 | #include "polarssl/md5.h" |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 35 | |
| 36 | int x509_write_pubkey_der( unsigned char *buf, size_t size, rsa_context *rsa ) |
| 37 | { |
| 38 | int ret; |
| 39 | unsigned char *c; |
| 40 | size_t len = 0; |
| 41 | |
| 42 | c = buf + size - 1; |
| 43 | |
| 44 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->E ) ); |
| 45 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->N ) ); |
| 46 | |
| 47 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 48 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 49 | |
| 50 | if( c - buf < 1 ) |
| 51 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 52 | |
| 53 | *--c = 0; |
| 54 | len += 1; |
| 55 | |
| 56 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 57 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_BIT_STRING ) ); |
| 58 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 59 | ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, buf, (char *) OID_PKCS1_RSA ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 60 | |
| 61 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 62 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 63 | |
| 64 | return( len ); |
| 65 | } |
| 66 | |
| 67 | int x509_write_key_der( unsigned char *buf, size_t size, rsa_context *rsa ) |
| 68 | { |
| 69 | int ret; |
| 70 | unsigned char *c; |
| 71 | size_t len = 0; |
| 72 | |
| 73 | c = buf + size - 1; |
| 74 | |
| 75 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->QP ) ); |
| 76 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->DQ ) ); |
| 77 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->DP ) ); |
| 78 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->Q ) ); |
| 79 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->P ) ); |
| 80 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->D ) ); |
| 81 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->E ) ); |
| 82 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->N ) ); |
| 83 | ASN1_CHK_ADD( len, asn1_write_int( &c, buf, 0 ) ); |
| 84 | |
| 85 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 86 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 87 | |
| 88 | // TODO: Make NON RSA Specific variant later on |
| 89 | /* *--c = 0; |
| 90 | len += 1; |
| 91 | |
| 92 | len += asn1_write_len( &c, len); |
| 93 | len += asn1_write_tag( &c, ASN1_BIT_STRING ); |
| 94 | |
| 95 | len += asn1_write_oid( &c, OID_PKCS1_RSA ); |
| 96 | |
| 97 | len += asn1_write_int( &c, 0 ); |
| 98 | |
| 99 | len += asn1_write_len( &c, len); |
| 100 | len += asn1_write_tag( &c, ASN1_CONSTRUCTED | ASN1_SEQUENCE );*/ |
| 101 | |
| 102 | /* for(i = 0; i < len; ++i) |
| 103 | { |
| 104 | if (i % 16 == 0 ) printf("\n"); |
| 105 | printf("%02x ", c[i]); |
| 106 | } |
| 107 | printf("\n");*/ |
| 108 | |
| 109 | return( len ); |
| 110 | } |
| 111 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 112 | static int x509_write_name( unsigned char **p, unsigned char *start, char *oid, |
| 113 | char *name ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 114 | { |
| 115 | int ret; |
| 116 | size_t string_len = 0; |
| 117 | size_t oid_len = 0; |
| 118 | size_t len = 0; |
| 119 | |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 120 | // Write PrintableString for all except OID_PKCS9_EMAIL |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 121 | // |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 122 | if( OID_SIZE( OID_PKCS9_EMAIL ) == strlen( oid ) && |
| 123 | memcmp( oid, OID_PKCS9_EMAIL, strlen( oid ) ) == 0 ) |
| 124 | { |
| 125 | ASN1_CHK_ADD( string_len, asn1_write_ia5_string( p, start, name ) ); |
| 126 | } |
| 127 | else |
| 128 | ASN1_CHK_ADD( string_len, asn1_write_printable_string( p, start, name ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 129 | |
| 130 | // Write OID |
| 131 | // |
| 132 | ASN1_CHK_ADD( oid_len, asn1_write_oid( p, start, oid ) ); |
| 133 | |
| 134 | len = oid_len + string_len; |
| 135 | ASN1_CHK_ADD( len, asn1_write_len( p, start, oid_len + string_len ) ); |
| 136 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 137 | |
| 138 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 139 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SET ) ); |
| 140 | |
| 141 | return( len ); |
| 142 | } |
| 143 | |
Paul Bakker | 3cac5e0 | 2012-02-16 14:08:06 +0000 | [diff] [blame] | 144 | /* |
| 145 | * Wrapper for x509 hashes. |
Paul Bakker | 3cac5e0 | 2012-02-16 14:08:06 +0000 | [diff] [blame] | 146 | */ |
| 147 | static void x509_hash( const unsigned char *in, size_t len, int alg, |
| 148 | unsigned char *out ) |
| 149 | { |
| 150 | switch( alg ) |
| 151 | { |
| 152 | #if defined(POLARSSL_MD2_C) |
| 153 | case SIG_RSA_MD2 : md2( in, len, out ); break; |
| 154 | #endif |
| 155 | #if defined(POLARSSL_MD4_C) |
| 156 | case SIG_RSA_MD4 : md4( in, len, out ); break; |
| 157 | #endif |
| 158 | #if defined(POLARSSL_MD5_C) |
| 159 | case SIG_RSA_MD5 : md5( in, len, out ); break; |
| 160 | #endif |
| 161 | #if defined(POLARSSL_SHA1_C) |
| 162 | case SIG_RSA_SHA1 : sha1( in, len, out ); break; |
| 163 | #endif |
| 164 | #if defined(POLARSSL_SHA2_C) |
| 165 | case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break; |
| 166 | case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break; |
| 167 | #endif |
| 168 | #if defined(POLARSSL_SHA4_C) |
| 169 | case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break; |
| 170 | case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break; |
| 171 | #endif |
| 172 | default: |
| 173 | memset( out, '\xFF', 64 ); |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 178 | static int x509_write_sig( unsigned char **p, unsigned char *start, char *oid, |
| 179 | unsigned char *sig, size_t size ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 180 | { |
| 181 | int ret; |
| 182 | size_t len = 0; |
| 183 | |
| 184 | if( *p - start < (int) size + 1 ) |
| 185 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 186 | |
| 187 | len = size; |
| 188 | (*p) -= len; |
| 189 | memcpy( *p, sig, len ); |
| 190 | |
| 191 | *--(*p) = 0; |
| 192 | len += 1; |
| 193 | |
| 194 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 195 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) ); |
| 196 | |
| 197 | // Write OID |
| 198 | // |
| 199 | ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid ) ); |
| 200 | |
| 201 | return( len ); |
| 202 | } |
| 203 | |
| 204 | int x509_write_cert_req( unsigned char *buf, size_t size, rsa_context *rsa, |
Paul Bakker | 3cac5e0 | 2012-02-16 14:08:06 +0000 | [diff] [blame] | 205 | x509_req_name *req_name, int hash_id ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 206 | { |
| 207 | int ret; |
Paul Bakker | 3cac5e0 | 2012-02-16 14:08:06 +0000 | [diff] [blame] | 208 | char sig_oid[10]; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 209 | unsigned char *c, *c2; |
Paul Bakker | 3cac5e0 | 2012-02-16 14:08:06 +0000 | [diff] [blame] | 210 | unsigned char hash[64]; |
| 211 | unsigned char sig[POLARSSL_MPI_MAX_SIZE]; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 212 | unsigned char tmp_buf[2048]; |
| 213 | size_t sub_len = 0, pub_len = 0, sig_len = 0; |
| 214 | size_t len = 0; |
| 215 | x509_req_name *cur = req_name; |
| 216 | |
| 217 | c = tmp_buf + 2048 - 1; |
| 218 | |
| 219 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, 0 ) ); |
| 220 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC ) ); |
| 221 | |
| 222 | ASN1_CHK_ADD( pub_len, asn1_write_mpi( &c, tmp_buf, &rsa->E ) ); |
| 223 | ASN1_CHK_ADD( pub_len, asn1_write_mpi( &c, tmp_buf, &rsa->N ) ); |
| 224 | |
| 225 | ASN1_CHK_ADD( pub_len, asn1_write_len( &c, tmp_buf, pub_len ) ); |
| 226 | ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 227 | |
| 228 | if( c - tmp_buf < 1 ) |
| 229 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 230 | |
| 231 | *--c = 0; |
| 232 | pub_len += 1; |
| 233 | |
| 234 | ASN1_CHK_ADD( pub_len, asn1_write_len( &c, tmp_buf, pub_len ) ); |
| 235 | ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, tmp_buf, ASN1_BIT_STRING ) ); |
| 236 | |
Paul Bakker | 1d073c5 | 2014-07-08 20:15:51 +0200 | [diff] [blame] | 237 | ASN1_CHK_ADD( pub_len, asn1_write_algorithm_identifier( &c, tmp_buf, (char *) OID_PKCS1_RSA ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 238 | |
| 239 | len += pub_len; |
| 240 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, pub_len ) ); |
| 241 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 242 | |
| 243 | while( cur != NULL ) |
| 244 | { |
| 245 | ASN1_CHK_ADD( sub_len, x509_write_name( &c, tmp_buf, cur->oid, cur->name ) ); |
| 246 | |
| 247 | cur = cur->next; |
| 248 | } |
| 249 | |
| 250 | len += sub_len; |
| 251 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) ); |
| 252 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 253 | |
| 254 | ASN1_CHK_ADD( len, asn1_write_int( &c, tmp_buf, 0 ) ); |
| 255 | |
| 256 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) ); |
| 257 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 258 | |
Paul Bakker | 3cac5e0 | 2012-02-16 14:08:06 +0000 | [diff] [blame] | 259 | x509_hash( c, len, hash_id, hash ); |
| 260 | |
| 261 | rsa_pkcs1_sign( rsa, NULL, NULL, RSA_PRIVATE, hash_id, 0, hash, sig ); |
| 262 | |
| 263 | // Generate correct OID |
| 264 | // |
| 265 | memcpy( sig_oid, OID_PKCS1, 8 ); |
| 266 | sig_oid[8] = hash_id; |
| 267 | sig_oid[9] = '\0'; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 268 | |
| 269 | c2 = buf + size - 1; |
Paul Bakker | 3cac5e0 | 2012-02-16 14:08:06 +0000 | [diff] [blame] | 270 | ASN1_CHK_ADD( sig_len, x509_write_sig( &c2, buf, sig_oid, sig, rsa->len ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 271 | |
| 272 | c2 -= len; |
| 273 | memcpy( c2, c, len ); |
| 274 | |
| 275 | len += sig_len; |
| 276 | ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) ); |
| 277 | ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 278 | |
| 279 | return( len ); |
| 280 | } |
| 281 | |
| 282 | #endif |