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" |
| 34 | |
| 35 | int x509_write_pubkey_der( unsigned char *buf, size_t size, rsa_context *rsa ) |
| 36 | { |
| 37 | int ret; |
| 38 | unsigned char *c; |
| 39 | size_t len = 0; |
| 40 | |
| 41 | c = buf + size - 1; |
| 42 | |
| 43 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->E ) ); |
| 44 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->N ) ); |
| 45 | |
| 46 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 47 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 48 | |
| 49 | if( c - buf < 1 ) |
| 50 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 51 | |
| 52 | *--c = 0; |
| 53 | len += 1; |
| 54 | |
| 55 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 56 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_BIT_STRING ) ); |
| 57 | |
| 58 | ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, buf, OID_PKCS1_RSA ) ); |
| 59 | |
| 60 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 61 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 62 | |
| 63 | return( len ); |
| 64 | } |
| 65 | |
| 66 | int x509_write_key_der( unsigned char *buf, size_t size, rsa_context *rsa ) |
| 67 | { |
| 68 | int ret; |
| 69 | unsigned char *c; |
| 70 | size_t len = 0; |
| 71 | |
| 72 | c = buf + size - 1; |
| 73 | |
| 74 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->QP ) ); |
| 75 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->DQ ) ); |
| 76 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->DP ) ); |
| 77 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->Q ) ); |
| 78 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->P ) ); |
| 79 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->D ) ); |
| 80 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->E ) ); |
| 81 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->N ) ); |
| 82 | ASN1_CHK_ADD( len, asn1_write_int( &c, buf, 0 ) ); |
| 83 | |
| 84 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 85 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 86 | |
| 87 | // TODO: Make NON RSA Specific variant later on |
| 88 | /* *--c = 0; |
| 89 | len += 1; |
| 90 | |
| 91 | len += asn1_write_len( &c, len); |
| 92 | len += asn1_write_tag( &c, ASN1_BIT_STRING ); |
| 93 | |
| 94 | len += asn1_write_oid( &c, OID_PKCS1_RSA ); |
| 95 | |
| 96 | len += asn1_write_int( &c, 0 ); |
| 97 | |
| 98 | len += asn1_write_len( &c, len); |
| 99 | len += asn1_write_tag( &c, ASN1_CONSTRUCTED | ASN1_SEQUENCE );*/ |
| 100 | |
| 101 | /* for(i = 0; i < len; ++i) |
| 102 | { |
| 103 | if (i % 16 == 0 ) printf("\n"); |
| 104 | printf("%02x ", c[i]); |
| 105 | } |
| 106 | printf("\n");*/ |
| 107 | |
| 108 | return( len ); |
| 109 | } |
| 110 | |
| 111 | int x509_write_name( unsigned char **p, unsigned char *start, char *oid, |
| 112 | char *name ) |
| 113 | { |
| 114 | int ret; |
| 115 | size_t string_len = 0; |
| 116 | size_t oid_len = 0; |
| 117 | size_t len = 0; |
| 118 | |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 119 | // Write PrintableString for all except OID_PKCS9_EMAIL |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 120 | // |
Paul Bakker | 0588815 | 2012-02-16 10:26:57 +0000 | [diff] [blame] | 121 | if( OID_SIZE( OID_PKCS9_EMAIL ) == strlen( oid ) && |
| 122 | memcmp( oid, OID_PKCS9_EMAIL, strlen( oid ) ) == 0 ) |
| 123 | { |
| 124 | ASN1_CHK_ADD( string_len, asn1_write_ia5_string( p, start, name ) ); |
| 125 | } |
| 126 | else |
| 127 | ASN1_CHK_ADD( string_len, asn1_write_printable_string( p, start, name ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 128 | |
| 129 | // Write OID |
| 130 | // |
| 131 | ASN1_CHK_ADD( oid_len, asn1_write_oid( p, start, oid ) ); |
| 132 | |
| 133 | len = oid_len + string_len; |
| 134 | ASN1_CHK_ADD( len, asn1_write_len( p, start, oid_len + string_len ) ); |
| 135 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 136 | |
| 137 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 138 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SET ) ); |
| 139 | |
| 140 | return( len ); |
| 141 | } |
| 142 | |
| 143 | int x509_write_sig( unsigned char **p, unsigned char *start, char *oid, |
| 144 | unsigned char *sig, size_t size ) |
| 145 | { |
| 146 | int ret; |
| 147 | size_t len = 0; |
| 148 | |
| 149 | if( *p - start < (int) size + 1 ) |
| 150 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 151 | |
| 152 | len = size; |
| 153 | (*p) -= len; |
| 154 | memcpy( *p, sig, len ); |
| 155 | |
| 156 | *--(*p) = 0; |
| 157 | len += 1; |
| 158 | |
| 159 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 160 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) ); |
| 161 | |
| 162 | // Write OID |
| 163 | // |
| 164 | ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid ) ); |
| 165 | |
| 166 | return( len ); |
| 167 | } |
| 168 | |
| 169 | int x509_write_cert_req( unsigned char *buf, size_t size, rsa_context *rsa, |
| 170 | x509_req_name *req_name ) |
| 171 | { |
| 172 | int ret; |
| 173 | unsigned char *c, *c2; |
| 174 | unsigned char hash[20]; |
| 175 | unsigned char sig[512]; |
| 176 | unsigned char tmp_buf[2048]; |
| 177 | size_t sub_len = 0, pub_len = 0, sig_len = 0; |
| 178 | size_t len = 0; |
| 179 | x509_req_name *cur = req_name; |
| 180 | |
| 181 | c = tmp_buf + 2048 - 1; |
| 182 | |
| 183 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, 0 ) ); |
| 184 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC ) ); |
| 185 | |
| 186 | ASN1_CHK_ADD( pub_len, asn1_write_mpi( &c, tmp_buf, &rsa->E ) ); |
| 187 | ASN1_CHK_ADD( pub_len, asn1_write_mpi( &c, tmp_buf, &rsa->N ) ); |
| 188 | |
| 189 | ASN1_CHK_ADD( pub_len, asn1_write_len( &c, tmp_buf, pub_len ) ); |
| 190 | ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 191 | |
| 192 | if( c - tmp_buf < 1 ) |
| 193 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 194 | |
| 195 | *--c = 0; |
| 196 | pub_len += 1; |
| 197 | |
| 198 | ASN1_CHK_ADD( pub_len, asn1_write_len( &c, tmp_buf, pub_len ) ); |
| 199 | ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, tmp_buf, ASN1_BIT_STRING ) ); |
| 200 | |
| 201 | ASN1_CHK_ADD( pub_len, asn1_write_algorithm_identifier( &c, tmp_buf, OID_PKCS1_RSA ) ); |
| 202 | |
| 203 | len += pub_len; |
| 204 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, pub_len ) ); |
| 205 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 206 | |
| 207 | while( cur != NULL ) |
| 208 | { |
| 209 | ASN1_CHK_ADD( sub_len, x509_write_name( &c, tmp_buf, cur->oid, cur->name ) ); |
| 210 | |
| 211 | cur = cur->next; |
| 212 | } |
| 213 | |
| 214 | len += sub_len; |
| 215 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) ); |
| 216 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 217 | |
| 218 | ASN1_CHK_ADD( len, asn1_write_int( &c, tmp_buf, 0 ) ); |
| 219 | |
| 220 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) ); |
| 221 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 222 | |
| 223 | sha1( c, len, hash ); |
| 224 | rsa_pkcs1_sign( rsa, NULL, NULL, RSA_PRIVATE, SIG_RSA_SHA1, 0, hash, sig ); |
| 225 | |
| 226 | c2 = buf + size - 1; |
| 227 | ASN1_CHK_ADD( sig_len, x509_write_sig( &c2, buf, OID_PKCS1_SHA1, sig, rsa->len ) ); |
| 228 | |
| 229 | c2 -= len; |
| 230 | memcpy( c2, c, len ); |
| 231 | |
| 232 | len += sig_len; |
| 233 | ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) ); |
| 234 | ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 235 | |
| 236 | return( len ); |
| 237 | } |
| 238 | |
| 239 | #endif |