Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * X509 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_X509_WRITE_C) |
| 29 | |
| 30 | #include "polarssl/asn1write.h" |
| 31 | #include "polarssl/x509write.h" |
| 32 | #include "polarssl/x509.h" |
| 33 | #include "polarssl/sha1.h" |
Paul Bakker | 3cac5e0 | 2012-02-16 14:08:06 +0000 | [diff] [blame^] | 34 | #include "polarssl/sha2.h" |
| 35 | #include "polarssl/sha4.h" |
| 36 | #include "polarssl/md4.h" |
| 37 | #include "polarssl/md5.h" |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 38 | |
| 39 | int x509_write_pubkey_der( unsigned char *buf, size_t size, rsa_context *rsa ) |
| 40 | { |
| 41 | int ret; |
| 42 | unsigned char *c; |
| 43 | size_t len = 0; |
| 44 | |
| 45 | c = buf + size - 1; |
| 46 | |
| 47 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->E ) ); |
| 48 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->N ) ); |
| 49 | |
| 50 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 51 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 52 | |
| 53 | if( c - buf < 1 ) |
| 54 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 55 | |
| 56 | *--c = 0; |
| 57 | len += 1; |
| 58 | |
| 59 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 60 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_BIT_STRING ) ); |
| 61 | |
| 62 | ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, buf, OID_PKCS1_RSA ) ); |
| 63 | |
| 64 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 65 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 66 | |
| 67 | return( len ); |
| 68 | } |
| 69 | |
| 70 | int x509_write_key_der( unsigned char *buf, size_t size, rsa_context *rsa ) |
| 71 | { |
| 72 | int ret; |
| 73 | unsigned char *c; |
| 74 | size_t len = 0; |
| 75 | |
| 76 | c = buf + size - 1; |
| 77 | |
| 78 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->QP ) ); |
| 79 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->DQ ) ); |
| 80 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->DP ) ); |
| 81 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->Q ) ); |
| 82 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->P ) ); |
| 83 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->D ) ); |
| 84 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->E ) ); |
| 85 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->N ) ); |
| 86 | ASN1_CHK_ADD( len, asn1_write_int( &c, buf, 0 ) ); |
| 87 | |
| 88 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 89 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 90 | |
| 91 | // TODO: Make NON RSA Specific variant later on |
| 92 | /* *--c = 0; |
| 93 | len += 1; |
| 94 | |
| 95 | len += asn1_write_len( &c, len); |
| 96 | len += asn1_write_tag( &c, ASN1_BIT_STRING ); |
| 97 | |
| 98 | len += asn1_write_oid( &c, OID_PKCS1_RSA ); |
| 99 | |
| 100 | len += asn1_write_int( &c, 0 ); |
| 101 | |
| 102 | len += asn1_write_len( &c, len); |
| 103 | len += asn1_write_tag( &c, ASN1_CONSTRUCTED | ASN1_SEQUENCE );*/ |
| 104 | |
| 105 | /* for(i = 0; i < len; ++i) |
| 106 | { |
| 107 | if (i % 16 == 0 ) printf("\n"); |
| 108 | printf("%02x ", c[i]); |
| 109 | } |
| 110 | printf("\n");*/ |
| 111 | |
| 112 | return( len ); |
| 113 | } |
| 114 | |
| 115 | int x509_write_name( unsigned char **p, unsigned char *start, char *oid, |
| 116 | char *name ) |
| 117 | { |
| 118 | int ret; |
| 119 | size_t string_len = 0; |
| 120 | size_t oid_len = 0; |
| 121 | size_t len = 0; |
| 122 | |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 123 | // Write PrintableString for all except OID_PKCS9_EMAIL |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 124 | // |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 125 | if( OID_SIZE( OID_PKCS9_EMAIL ) == strlen( oid ) && |
| 126 | memcmp( oid, OID_PKCS9_EMAIL, strlen( oid ) ) == 0 ) |
| 127 | { |
| 128 | ASN1_CHK_ADD( string_len, asn1_write_ia5_string( p, start, name ) ); |
| 129 | } |
| 130 | else |
| 131 | ASN1_CHK_ADD( string_len, asn1_write_printable_string( p, start, name ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 132 | |
| 133 | // Write OID |
| 134 | // |
| 135 | ASN1_CHK_ADD( oid_len, asn1_write_oid( p, start, oid ) ); |
| 136 | |
| 137 | len = oid_len + string_len; |
| 138 | ASN1_CHK_ADD( len, asn1_write_len( p, start, oid_len + string_len ) ); |
| 139 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 140 | |
| 141 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 142 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SET ) ); |
| 143 | |
| 144 | return( len ); |
| 145 | } |
| 146 | |
Paul Bakker | 3cac5e0 | 2012-02-16 14:08:06 +0000 | [diff] [blame^] | 147 | /* |
| 148 | * Wrapper for x509 hashes. |
| 149 | * |
| 150 | * \param out Buffer to receive the hash (Should be at least 64 bytes) |
| 151 | */ |
| 152 | static void x509_hash( const unsigned char *in, size_t len, int alg, |
| 153 | unsigned char *out ) |
| 154 | { |
| 155 | switch( alg ) |
| 156 | { |
| 157 | #if defined(POLARSSL_MD2_C) |
| 158 | case SIG_RSA_MD2 : md2( in, len, out ); break; |
| 159 | #endif |
| 160 | #if defined(POLARSSL_MD4_C) |
| 161 | case SIG_RSA_MD4 : md4( in, len, out ); break; |
| 162 | #endif |
| 163 | #if defined(POLARSSL_MD5_C) |
| 164 | case SIG_RSA_MD5 : md5( in, len, out ); break; |
| 165 | #endif |
| 166 | #if defined(POLARSSL_SHA1_C) |
| 167 | case SIG_RSA_SHA1 : sha1( in, len, out ); break; |
| 168 | #endif |
| 169 | #if defined(POLARSSL_SHA2_C) |
| 170 | case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break; |
| 171 | case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break; |
| 172 | #endif |
| 173 | #if defined(POLARSSL_SHA4_C) |
| 174 | case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break; |
| 175 | case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break; |
| 176 | #endif |
| 177 | default: |
| 178 | memset( out, '\xFF', 64 ); |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 183 | int x509_write_sig( unsigned char **p, unsigned char *start, char *oid, |
| 184 | unsigned char *sig, size_t size ) |
| 185 | { |
| 186 | int ret; |
| 187 | size_t len = 0; |
| 188 | |
| 189 | if( *p - start < (int) size + 1 ) |
| 190 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 191 | |
| 192 | len = size; |
| 193 | (*p) -= len; |
| 194 | memcpy( *p, sig, len ); |
| 195 | |
| 196 | *--(*p) = 0; |
| 197 | len += 1; |
| 198 | |
| 199 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 200 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) ); |
| 201 | |
| 202 | // Write OID |
| 203 | // |
| 204 | ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid ) ); |
| 205 | |
| 206 | return( len ); |
| 207 | } |
| 208 | |
| 209 | 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^] | 210 | x509_req_name *req_name, int hash_id ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 211 | { |
| 212 | int ret; |
Paul Bakker | 3cac5e0 | 2012-02-16 14:08:06 +0000 | [diff] [blame^] | 213 | char sig_oid[10]; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 214 | unsigned char *c, *c2; |
Paul Bakker | 3cac5e0 | 2012-02-16 14:08:06 +0000 | [diff] [blame^] | 215 | unsigned char hash[64]; |
| 216 | unsigned char sig[POLARSSL_MPI_MAX_SIZE]; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 217 | unsigned char tmp_buf[2048]; |
| 218 | size_t sub_len = 0, pub_len = 0, sig_len = 0; |
| 219 | size_t len = 0; |
| 220 | x509_req_name *cur = req_name; |
| 221 | |
| 222 | c = tmp_buf + 2048 - 1; |
| 223 | |
| 224 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, 0 ) ); |
| 225 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC ) ); |
| 226 | |
| 227 | ASN1_CHK_ADD( pub_len, asn1_write_mpi( &c, tmp_buf, &rsa->E ) ); |
| 228 | ASN1_CHK_ADD( pub_len, asn1_write_mpi( &c, tmp_buf, &rsa->N ) ); |
| 229 | |
| 230 | ASN1_CHK_ADD( pub_len, asn1_write_len( &c, tmp_buf, pub_len ) ); |
| 231 | ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 232 | |
| 233 | if( c - tmp_buf < 1 ) |
| 234 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 235 | |
| 236 | *--c = 0; |
| 237 | pub_len += 1; |
| 238 | |
| 239 | ASN1_CHK_ADD( pub_len, asn1_write_len( &c, tmp_buf, pub_len ) ); |
| 240 | ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, tmp_buf, ASN1_BIT_STRING ) ); |
| 241 | |
| 242 | ASN1_CHK_ADD( pub_len, asn1_write_algorithm_identifier( &c, tmp_buf, OID_PKCS1_RSA ) ); |
| 243 | |
| 244 | len += pub_len; |
| 245 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, pub_len ) ); |
| 246 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 247 | |
| 248 | while( cur != NULL ) |
| 249 | { |
| 250 | ASN1_CHK_ADD( sub_len, x509_write_name( &c, tmp_buf, cur->oid, cur->name ) ); |
| 251 | |
| 252 | cur = cur->next; |
| 253 | } |
| 254 | |
| 255 | len += sub_len; |
| 256 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) ); |
| 257 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 258 | |
| 259 | ASN1_CHK_ADD( len, asn1_write_int( &c, tmp_buf, 0 ) ); |
| 260 | |
| 261 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) ); |
| 262 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 263 | |
Paul Bakker | 3cac5e0 | 2012-02-16 14:08:06 +0000 | [diff] [blame^] | 264 | x509_hash( c, len, hash_id, hash ); |
| 265 | |
| 266 | rsa_pkcs1_sign( rsa, NULL, NULL, RSA_PRIVATE, hash_id, 0, hash, sig ); |
| 267 | |
| 268 | // Generate correct OID |
| 269 | // |
| 270 | memcpy( sig_oid, OID_PKCS1, 8 ); |
| 271 | sig_oid[8] = hash_id; |
| 272 | sig_oid[9] = '\0'; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 273 | |
| 274 | c2 = buf + size - 1; |
Paul Bakker | 3cac5e0 | 2012-02-16 14:08:06 +0000 | [diff] [blame^] | 275 | 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] | 276 | |
| 277 | c2 -= len; |
| 278 | memcpy( c2, c, len ); |
| 279 | |
| 280 | len += sig_len; |
| 281 | ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) ); |
| 282 | ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 283 | |
| 284 | return( len ); |
| 285 | } |
| 286 | |
| 287 | #endif |