blob: b114e700e55a34fc040f56e7aa2f94b355074f00 [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"
33#include "polarssl/sha1.h"
34
35int 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
66int 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
111int 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
119 // Write PrintableString
120 //
121 ASN1_CHK_ADD( string_len, asn1_write_printable_string( p, start, name ) );
122
123 // Write OID
124 //
125 ASN1_CHK_ADD( oid_len, asn1_write_oid( p, start, oid ) );
126
127 len = oid_len + string_len;
128 ASN1_CHK_ADD( len, asn1_write_len( p, start, oid_len + string_len ) );
129 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
130
131 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
132 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SET ) );
133
134 return( len );
135}
136
137int x509_write_sig( unsigned char **p, unsigned char *start, char *oid,
138 unsigned char *sig, size_t size )
139{
140 int ret;
141 size_t len = 0;
142
143 if( *p - start < (int) size + 1 )
144 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
145
146 len = size;
147 (*p) -= len;
148 memcpy( *p, sig, len );
149
150 *--(*p) = 0;
151 len += 1;
152
153 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
154 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
155
156 // Write OID
157 //
158 ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid ) );
159
160 return( len );
161}
162
163int x509_write_cert_req( unsigned char *buf, size_t size, rsa_context *rsa,
164 x509_req_name *req_name )
165{
166 int ret;
167 unsigned char *c, *c2;
168 unsigned char hash[20];
169 unsigned char sig[512];
170 unsigned char tmp_buf[2048];
171 size_t sub_len = 0, pub_len = 0, sig_len = 0;
172 size_t len = 0;
173 x509_req_name *cur = req_name;
174
175 c = tmp_buf + 2048 - 1;
176
177 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, 0 ) );
178 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC ) );
179
180 ASN1_CHK_ADD( pub_len, asn1_write_mpi( &c, tmp_buf, &rsa->E ) );
181 ASN1_CHK_ADD( pub_len, asn1_write_mpi( &c, tmp_buf, &rsa->N ) );
182
183 ASN1_CHK_ADD( pub_len, asn1_write_len( &c, tmp_buf, pub_len ) );
184 ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
185
186 if( c - tmp_buf < 1 )
187 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
188
189 *--c = 0;
190 pub_len += 1;
191
192 ASN1_CHK_ADD( pub_len, asn1_write_len( &c, tmp_buf, pub_len ) );
193 ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, tmp_buf, ASN1_BIT_STRING ) );
194
195 ASN1_CHK_ADD( pub_len, asn1_write_algorithm_identifier( &c, tmp_buf, OID_PKCS1_RSA ) );
196
197 len += pub_len;
198 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, pub_len ) );
199 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
200
201 while( cur != NULL )
202 {
203 ASN1_CHK_ADD( sub_len, x509_write_name( &c, tmp_buf, cur->oid, cur->name ) );
204
205 cur = cur->next;
206 }
207
208 len += sub_len;
209 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
210 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
211
212 ASN1_CHK_ADD( len, asn1_write_int( &c, tmp_buf, 0 ) );
213
214 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
215 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
216
217 sha1( c, len, hash );
218 rsa_pkcs1_sign( rsa, NULL, NULL, RSA_PRIVATE, SIG_RSA_SHA1, 0, hash, sig );
219
220 c2 = buf + size - 1;
221 ASN1_CHK_ADD( sig_len, x509_write_sig( &c2, buf, OID_PKCS1_SHA1, sig, rsa->len ) );
222
223 c2 -= len;
224 memcpy( c2, c, len );
225
226 len += sig_len;
227 ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
228 ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
229
230 return( len );
231}
232
233#endif