PolarSSL v1.2.5
x509write.c
Go to the documentation of this file.
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 #include "polarssl/sha2.h"
35 #include "polarssl/sha4.h"
36 #include "polarssl/md4.h"
37 #include "polarssl/md5.h"
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 ) );
52 
53  if( c - buf < 1 )
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 
63 
64  ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
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 ) );
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 
123  // Write PrintableString for all except OID_PKCS9_EMAIL
124  //
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 ) );
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 ) );
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 
147 /*
148  * Wrapper for x509 hashes.
149  */
150 static void x509_hash( const unsigned char *in, size_t len, int alg,
151  unsigned char *out )
152 {
153  switch( alg )
154  {
155 #if defined(POLARSSL_MD2_C)
156  case SIG_RSA_MD2 : md2( in, len, out ); break;
157 #endif
158 #if defined(POLARSSL_MD4_C)
159  case SIG_RSA_MD4 : md4( in, len, out ); break;
160 #endif
161 #if defined(POLARSSL_MD5_C)
162  case SIG_RSA_MD5 : md5( in, len, out ); break;
163 #endif
164 #if defined(POLARSSL_SHA1_C)
165  case SIG_RSA_SHA1 : sha1( in, len, out ); break;
166 #endif
167 #if defined(POLARSSL_SHA2_C)
168  case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
169  case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
170 #endif
171 #if defined(POLARSSL_SHA4_C)
172  case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
173  case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
174 #endif
175  default:
176  memset( out, '\xFF', 64 );
177  break;
178  }
179 }
180 
181 int x509_write_sig( unsigned char **p, unsigned char *start, char *oid,
182  unsigned char *sig, size_t size )
183 {
184  int ret;
185  size_t len = 0;
186 
187  if( *p - start < (int) size + 1 )
189 
190  len = size;
191  (*p) -= len;
192  memcpy( *p, sig, len );
193 
194  *--(*p) = 0;
195  len += 1;
196 
197  ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
198  ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
199 
200  // Write OID
201  //
202  ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid ) );
203 
204  return( len );
205 }
206 
207 int x509_write_cert_req( unsigned char *buf, size_t size, rsa_context *rsa,
208  x509_req_name *req_name, int hash_id )
209 {
210  int ret;
211  char sig_oid[10];
212  unsigned char *c, *c2;
213  unsigned char hash[64];
214  unsigned char sig[POLARSSL_MPI_MAX_SIZE];
215  unsigned char tmp_buf[2048];
216  size_t sub_len = 0, pub_len = 0, sig_len = 0;
217  size_t len = 0;
218  x509_req_name *cur = req_name;
219 
220  c = tmp_buf + 2048 - 1;
221 
222  ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, 0 ) );
224 
225  ASN1_CHK_ADD( pub_len, asn1_write_mpi( &c, tmp_buf, &rsa->E ) );
226  ASN1_CHK_ADD( pub_len, asn1_write_mpi( &c, tmp_buf, &rsa->N ) );
227 
228  ASN1_CHK_ADD( pub_len, asn1_write_len( &c, tmp_buf, pub_len ) );
229  ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
230 
231  if( c - tmp_buf < 1 )
233 
234  *--c = 0;
235  pub_len += 1;
236 
237  ASN1_CHK_ADD( pub_len, asn1_write_len( &c, tmp_buf, pub_len ) );
238  ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, tmp_buf, ASN1_BIT_STRING ) );
239 
240  ASN1_CHK_ADD( pub_len, asn1_write_algorithm_identifier( &c, tmp_buf, OID_PKCS1_RSA ) );
241 
242  len += pub_len;
243  ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, pub_len ) );
244  ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
245 
246  while( cur != NULL )
247  {
248  ASN1_CHK_ADD( sub_len, x509_write_name( &c, tmp_buf, cur->oid, cur->name ) );
249 
250  cur = cur->next;
251  }
252 
253  len += sub_len;
254  ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
255  ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
256 
257  ASN1_CHK_ADD( len, asn1_write_int( &c, tmp_buf, 0 ) );
258 
259  ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
260  ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
261 
262  x509_hash( c, len, hash_id, hash );
263 
264  rsa_pkcs1_sign( rsa, NULL, NULL, RSA_PRIVATE, hash_id, 0, hash, sig );
265 
266  // Generate correct OID
267  //
268  memcpy( sig_oid, OID_PKCS1, 8 );
269  sig_oid[8] = hash_id;
270  sig_oid[9] = '\0';
271 
272  c2 = buf + size - 1;
273  ASN1_CHK_ADD( sig_len, x509_write_sig( &c2, buf, sig_oid, sig, rsa->len ) );
274 
275  c2 -= len;
276  memcpy( c2, c, len );
277 
278  len += sig_len;
279  ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
281 
282  return( len );
283 }
284 
285 #endif