blob: a5c969367f64d2042601aa5d614bcb3378c92c98 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 Certificate Signing Request (CSR) parsing
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The ITU-T X.509 standard defines a certificate format for PKI.
24 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020025 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
26 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
27 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028 *
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
30 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
31 */
32
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
36#include POLARSSL_CONFIG_FILE
37#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038
39#if defined(POLARSSL_X509_CSR_PARSE_C)
40
41#include "polarssl/x509_csr.h"
42#include "polarssl/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000043
44#include <string.h>
45
Paul Bakker7c6b2c32013-09-16 13:49:26 +020046#if defined(POLARSSL_PEM_PARSE_C)
47#include "polarssl/pem.h"
48#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052#else
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <stdlib.h>
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#define polarssl_free free
Rich Evansfac657f2015-01-30 11:00:01 +000055#define polarssl_malloc malloc
56#define polarssl_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057#endif
58
Paul Bakkerfa6a6202013-10-28 18:48:30 +010059#if defined(POLARSSL_FS_IO) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#include <stdio.h>
61#endif
62
Paul Bakker34617722014-06-13 17:20:13 +020063/* Implementation that should never be optimized out by the compiler */
64static void polarssl_zeroize( void *v, size_t n ) {
65 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
66}
67
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068/*
69 * Version ::= INTEGER { v1(0) }
70 */
71static int x509_csr_get_version( unsigned char **p,
72 const unsigned char *end,
73 int *ver )
74{
75 int ret;
76
77 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
78 {
79 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
80 {
81 *ver = 0;
82 return( 0 );
83 }
84
Paul Bakker51876562013-09-17 14:36:05 +020085 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086 }
87
88 return( 0 );
89}
90
91/*
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020092 * Parse a CSR in DER format
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093 */
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +020094int x509_csr_parse_der( x509_csr *csr,
95 const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096{
97 int ret;
98 size_t len;
99 unsigned char *p, *end;
Manuel Pégourié-Gonnard39868ee2014-01-24 18:47:17 +0100100 x509_buf sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200102 memset( &sig_params, 0, sizeof( x509_buf ) );
103
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104 /*
105 * Check for valid input
106 */
107 if( csr == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200108 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109
Paul Bakker369d2eb2013-09-18 11:58:25 +0200110 x509_csr_init( csr );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200112 /*
113 * first copy the raw DER data
114 */
115 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200117 if( p == NULL )
118 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200120 memcpy( p, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200121
122 csr->raw.p = p;
123 csr->raw.len = len;
124 end = p + len;
125
126 /*
127 * CertificationRequest ::= SEQUENCE {
128 * certificationRequestInfo CertificationRequestInfo,
129 * signatureAlgorithm AlgorithmIdentifier,
130 * signature BIT STRING
131 * }
132 */
133 if( ( ret = asn1_get_tag( &p, end, &len,
134 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
135 {
136 x509_csr_free( csr );
Paul Bakker51876562013-09-17 14:36:05 +0200137 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138 }
139
140 if( len != (size_t) ( end - p ) )
141 {
142 x509_csr_free( csr );
Paul Bakker51876562013-09-17 14:36:05 +0200143 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200144 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
145 }
146
147 /*
148 * CertificationRequestInfo ::= SEQUENCE {
149 */
150 csr->cri.p = p;
151
152 if( ( ret = asn1_get_tag( &p, end, &len,
153 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
154 {
155 x509_csr_free( csr );
Paul Bakker51876562013-09-17 14:36:05 +0200156 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200157 }
158
159 end = p + len;
160 csr->cri.len = end - csr->cri.p;
161
162 /*
163 * Version ::= INTEGER { v1(0) }
164 */
165 if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 )
166 {
167 x509_csr_free( csr );
168 return( ret );
169 }
170
171 csr->version++;
172
173 if( csr->version != 1 )
174 {
175 x509_csr_free( csr );
Paul Bakker51876562013-09-17 14:36:05 +0200176 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200177 }
178
179 /*
180 * subject Name
181 */
182 csr->subject_raw.p = p;
183
184 if( ( ret = asn1_get_tag( &p, end, &len,
185 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
186 {
187 x509_csr_free( csr );
Paul Bakker51876562013-09-17 14:36:05 +0200188 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200189 }
190
191 if( ( ret = x509_get_name( &p, p + len, &csr->subject ) ) != 0 )
192 {
193 x509_csr_free( csr );
194 return( ret );
195 }
196
197 csr->subject_raw.len = p - csr->subject_raw.p;
198
199 /*
200 * subjectPKInfo SubjectPublicKeyInfo
201 */
Paul Bakkerda771152013-09-16 22:45:03 +0200202 if( ( ret = pk_parse_subpubkey( &p, end, &csr->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200203 {
204 x509_csr_free( csr );
205 return( ret );
206 }
207
208 /*
209 * attributes [0] Attributes
210 */
211 if( ( ret = asn1_get_tag( &p, end, &len,
212 ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC ) ) != 0 )
213 {
214 x509_csr_free( csr );
Paul Bakker51876562013-09-17 14:36:05 +0200215 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216 }
217 // TODO Parse Attributes / extension requests
218
219 p += len;
220
221 end = csr->raw.p + csr->raw.len;
222
223 /*
224 * signatureAlgorithm AlgorithmIdentifier,
225 * signature BIT STRING
226 */
Manuel Pégourié-Gonnard39868ee2014-01-24 18:47:17 +0100227 if( ( ret = x509_get_alg( &p, end, &csr->sig_oid, &sig_params ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200228 {
229 x509_csr_free( csr );
230 return( ret );
231 }
232
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100233 if( ( ret = x509_get_sig_alg( &csr->sig_oid, &sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200234 &csr->sig_md, &csr->sig_pk,
235 &csr->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236 {
237 x509_csr_free( csr );
Paul Bakker51876562013-09-17 14:36:05 +0200238 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200239 }
240
241 if( ( ret = x509_get_sig( &p, end, &csr->sig ) ) != 0 )
242 {
243 x509_csr_free( csr );
244 return( ret );
245 }
246
247 if( p != end )
248 {
249 x509_csr_free( csr );
Paul Bakker51876562013-09-17 14:36:05 +0200250 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200251 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
252 }
253
254 return( 0 );
255}
256
Manuel Pégourié-Gonnardf3b47242014-06-16 18:06:48 +0200257/*
258 * Parse a CSR, allowing for PEM or raw DER encoding
259 */
260int x509_csr_parse( x509_csr *csr, const unsigned char *buf, size_t buflen )
261{
262 int ret;
263#if defined(POLARSSL_PEM_PARSE_C)
264 size_t use_len;
265 pem_context pem;
266#endif
267
268 /*
269 * Check for valid input
270 */
271 if( csr == NULL || buf == NULL )
272 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
273
274#if defined(POLARSSL_PEM_PARSE_C)
275 pem_init( &pem );
276 ret = pem_read_buffer( &pem,
277 "-----BEGIN CERTIFICATE REQUEST-----",
278 "-----END CERTIFICATE REQUEST-----",
279 buf, NULL, 0, &use_len );
280
281 if( ret == 0 )
282 {
283 /*
284 * Was PEM encoded, parse the result
285 */
286 if( ( ret = x509_csr_parse_der( csr, pem.buf, pem.buflen ) ) != 0 )
287 return( ret );
288
289 pem_free( &pem );
290 return( 0 );
291 }
292 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
293 {
294 pem_free( &pem );
295 return( ret );
296 }
297 else
298#endif /* POLARSSL_PEM_PARSE_C */
299 return( x509_csr_parse_der( csr, buf, buflen ) );
300}
301
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200302#if defined(POLARSSL_FS_IO)
303/*
304 * Load a CSR into the structure
305 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200306int x509_csr_parse_file( x509_csr *csr, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307{
308 int ret;
309 size_t n;
310 unsigned char *buf;
311
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100312 if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313 return( ret );
314
Paul Bakkerddf26b42013-09-18 13:46:23 +0200315 ret = x509_csr_parse( csr, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200316
Paul Bakker34617722014-06-13 17:20:13 +0200317 polarssl_zeroize( buf, n + 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200318 polarssl_free( buf );
319
320 return( ret );
321}
322#endif /* POLARSSL_FS_IO */
323
Paul Bakker6edcd412013-10-29 15:22:54 +0100324#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
325 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200326#include <stdarg.h>
327
328#if !defined vsnprintf
329#define vsnprintf _vsnprintf
330#endif // vsnprintf
331
332/*
333 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
334 * Result value is not size of buffer needed, but -1 if no fit is possible.
335 *
336 * This fuction tries to 'fix' this by at least suggesting enlarging the
337 * size by 20.
338 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200339static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200340{
341 va_list ap;
342 int res = -1;
343
344 va_start( ap, format );
345
346 res = vsnprintf( str, size, format, ap );
347
348 va_end( ap );
349
350 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +0200351 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352 return( (int) size + 20 );
353
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200354 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355}
356
357#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200358#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359
360#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
361
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200362#define SAFE_SNPRINTF() \
363{ \
364 if( ret == -1 ) \
365 return( -1 ); \
366 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200367 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200368 p[n - 1] = '\0'; \
369 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
370 } \
371 \
372 n -= (unsigned int) ret; \
373 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374}
375
376#define BEFORE_COLON 14
377#define BC "14"
378/*
379 * Return an informational string about the CSR.
380 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200381int x509_csr_info( char *buf, size_t size, const char *prefix,
382 const x509_csr *csr )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383{
384 int ret;
385 size_t n;
386 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387 char key_size_str[BEFORE_COLON];
388
389 p = buf;
390 n = size;
391
Rich Evansfac657f2015-01-30 11:00:01 +0000392 ret = polarssl_snprintf( p, n, "%sCSR version : %d",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393 prefix, csr->version );
394 SAFE_SNPRINTF();
395
Rich Evansfac657f2015-01-30 11:00:01 +0000396 ret = polarssl_snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +0200398 ret = x509_dn_gets( p, n, &csr->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399 SAFE_SNPRINTF();
400
Rich Evansfac657f2015-01-30 11:00:01 +0000401 ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402 SAFE_SNPRINTF();
403
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200404 ret = x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +0200405 csr->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 SAFE_SNPRINTF();
407
408 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
409 pk_get_name( &csr->pk ) ) ) != 0 )
410 {
411 return( ret );
412 }
413
Rich Evansfac657f2015-01-30 11:00:01 +0000414 ret = polarssl_snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415 (int) pk_get_size( &csr->pk ) );
416 SAFE_SNPRINTF();
417
418 return( (int) ( size - n ) );
419}
420
421/*
Paul Bakker369d2eb2013-09-18 11:58:25 +0200422 * Initialize a CSR
423 */
424void x509_csr_init( x509_csr *csr )
425{
426 memset( csr, 0, sizeof(x509_csr) );
427}
428
429/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430 * Unallocate all CSR data
431 */
432void x509_csr_free( x509_csr *csr )
433{
434 x509_name *name_cur;
435 x509_name *name_prv;
436
437 if( csr == NULL )
438 return;
439
440 pk_free( &csr->pk );
441
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200442#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200443 polarssl_free( csr->sig_opts );
444#endif
445
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446 name_cur = csr->subject.next;
447 while( name_cur != NULL )
448 {
449 name_prv = name_cur;
450 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +0200451 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452 polarssl_free( name_prv );
453 }
454
455 if( csr->raw.p != NULL )
456 {
Paul Bakker34617722014-06-13 17:20:13 +0200457 polarssl_zeroize( csr->raw.p, csr->raw.len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458 polarssl_free( csr->raw.p );
459 }
460
Paul Bakker34617722014-06-13 17:20:13 +0200461 polarssl_zeroize( csr, sizeof( x509_csr ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462}
463
464#endif /* POLARSSL_X509_CSR_PARSE_C */