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