blob: 7aa65ce37c51be028f66117fcc0d72b42ceaf9c0 [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * X509 buffer writing functionality
3 *
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerbdb912d2012-02-13 23:11:30 +00005 *
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 Bakkerc70b9822013-04-07 22:00:46 +020033#include "polarssl/md.h"
34#include "polarssl/oid.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000035
36int x509_write_pubkey_der( unsigned char *buf, size_t size, rsa_context *rsa )
37{
38 int ret;
39 unsigned char *c;
40 size_t len = 0;
41
42 c = buf + size - 1;
43
44 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->E ) );
45 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->N ) );
46
47 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
48 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
49
50 if( c - buf < 1 )
51 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
52
53 *--c = 0;
54 len += 1;
55
56 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
57 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_BIT_STRING ) );
58
59 ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, buf, OID_PKCS1_RSA ) );
60
61 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
62 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
63
64 return( len );
65}
66
67int x509_write_key_der( unsigned char *buf, size_t size, rsa_context *rsa )
68{
69 int ret;
70 unsigned char *c;
71 size_t len = 0;
72
73 c = buf + size - 1;
74
75 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->QP ) );
76 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->DQ ) );
77 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->DP ) );
78 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->Q ) );
79 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->P ) );
80 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->D ) );
81 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->E ) );
82 ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->N ) );
83 ASN1_CHK_ADD( len, asn1_write_int( &c, buf, 0 ) );
84
85 ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
86 ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
87
88 // TODO: Make NON RSA Specific variant later on
89/* *--c = 0;
90 len += 1;
91
92 len += asn1_write_len( &c, len);
93 len += asn1_write_tag( &c, ASN1_BIT_STRING );
94
95 len += asn1_write_oid( &c, OID_PKCS1_RSA );
96
97 len += asn1_write_int( &c, 0 );
98
99 len += asn1_write_len( &c, len);
100 len += asn1_write_tag( &c, ASN1_CONSTRUCTED | ASN1_SEQUENCE );*/
101
102/* for(i = 0; i < len; ++i)
103 {
104 if (i % 16 == 0 ) printf("\n");
105 printf("%02x ", c[i]);
106 }
107 printf("\n");*/
108
109 return( len );
110}
111
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200112static int x509_write_name( unsigned char **p, unsigned char *start, char *oid,
113 char *name )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000114{
115 int ret;
116 size_t string_len = 0;
117 size_t oid_len = 0;
118 size_t len = 0;
119
Paul Bakker05888152012-02-16 10:26:57 +0000120 // Write PrintableString for all except OID_PKCS9_EMAIL
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000121 //
Paul Bakker05888152012-02-16 10:26:57 +0000122 if( OID_SIZE( OID_PKCS9_EMAIL ) == strlen( oid ) &&
123 memcmp( oid, OID_PKCS9_EMAIL, strlen( oid ) ) == 0 )
124 {
125 ASN1_CHK_ADD( string_len, asn1_write_ia5_string( p, start, name ) );
126 }
127 else
128 ASN1_CHK_ADD( string_len, asn1_write_printable_string( p, start, name ) );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000129
130 // Write OID
131 //
132 ASN1_CHK_ADD( oid_len, asn1_write_oid( p, start, oid ) );
133
134 len = oid_len + string_len;
135 ASN1_CHK_ADD( len, asn1_write_len( p, start, oid_len + string_len ) );
136 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
137
138 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
139 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SET ) );
140
141 return( len );
142}
143
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200144static int x509_write_sig( unsigned char **p, unsigned char *start,
145 const char *oid, unsigned char *sig, size_t size )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000146{
147 int ret;
148 size_t len = 0;
149
150 if( *p - start < (int) size + 1 )
151 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
152
153 len = size;
154 (*p) -= len;
155 memcpy( *p, sig, len );
156
157 *--(*p) = 0;
158 len += 1;
159
160 ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
161 ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
162
163 // Write OID
164 //
165 ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid ) );
166
167 return( len );
168}
169
170int x509_write_cert_req( unsigned char *buf, size_t size, rsa_context *rsa,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200171 x509_req_name *req_name, md_type_t md_alg )
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000172{
173 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200174 const char *sig_oid;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000175 unsigned char *c, *c2;
Paul Bakker3cac5e02012-02-16 14:08:06 +0000176 unsigned char hash[64];
177 unsigned char sig[POLARSSL_MPI_MAX_SIZE];
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000178 unsigned char tmp_buf[2048];
179 size_t sub_len = 0, pub_len = 0, sig_len = 0;
180 size_t len = 0;
181 x509_req_name *cur = req_name;
182
183 c = tmp_buf + 2048 - 1;
184
185 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, 0 ) );
186 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC ) );
187
188 ASN1_CHK_ADD( pub_len, asn1_write_mpi( &c, tmp_buf, &rsa->E ) );
189 ASN1_CHK_ADD( pub_len, asn1_write_mpi( &c, tmp_buf, &rsa->N ) );
190
191 ASN1_CHK_ADD( pub_len, asn1_write_len( &c, tmp_buf, pub_len ) );
192 ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
193
194 if( c - tmp_buf < 1 )
195 return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
196
197 *--c = 0;
198 pub_len += 1;
199
200 ASN1_CHK_ADD( pub_len, asn1_write_len( &c, tmp_buf, pub_len ) );
201 ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, tmp_buf, ASN1_BIT_STRING ) );
202
203 ASN1_CHK_ADD( pub_len, asn1_write_algorithm_identifier( &c, tmp_buf, OID_PKCS1_RSA ) );
204
205 len += pub_len;
206 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, pub_len ) );
207 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
208
209 while( cur != NULL )
210 {
211 ASN1_CHK_ADD( sub_len, x509_write_name( &c, tmp_buf, cur->oid, cur->name ) );
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200212
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000213 cur = cur->next;
214 }
215
216 len += sub_len;
217 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
218 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
219
220 ASN1_CHK_ADD( len, asn1_write_int( &c, tmp_buf, 0 ) );
221
222 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
223 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200224
Paul Bakkerc70b9822013-04-07 22:00:46 +0200225 md( md_info_from_type( md_alg ), c, len, hash );
Paul Bakker3cac5e02012-02-16 14:08:06 +0000226
Paul Bakkerc70b9822013-04-07 22:00:46 +0200227 rsa_pkcs1_sign( rsa, NULL, NULL, RSA_PRIVATE, md_alg, 0, hash, sig );
Paul Bakker3cac5e02012-02-16 14:08:06 +0000228
229 // Generate correct OID
230 //
Paul Bakkerc70b9822013-04-07 22:00:46 +0200231 ret = oid_get_oid_by_sig_alg( POLARSSL_PK_RSA, md_alg, &sig_oid );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000232
233 c2 = buf + size - 1;
Paul Bakker3cac5e02012-02-16 14:08:06 +0000234 ASN1_CHK_ADD( sig_len, x509_write_sig( &c2, buf, sig_oid, sig, rsa->len ) );
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200235
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000236 c2 -= len;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200237 memcpy( c2, c, len );
238
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000239 len += sig_len;
240 ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
241 ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
242
243 return( len );
244}
245
246#endif