fuck! don't perform ssl handshake for blocked hosts!
[mediator.git] / src / libpolarssl / x509_csr.c
blob558b078b73eb169ad4d8e6186c7eff8d9d76ff95
1 /*
2 * X.509 Certificate Signing Request (CSR) parsing
4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
6 * This file is part of mbed TLS (https://tls.mbed.org)
8 * 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.
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.
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.
23 * The ITU-T X.509 standard defines a certificate format for PKI.
25 * 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)
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
33 #if !defined(POLARSSL_CONFIG_FILE)
34 #include "polarssl/config.h"
35 #else
36 #include POLARSSL_CONFIG_FILE
37 #endif
39 #if defined(POLARSSL_X509_CSR_PARSE_C)
41 #include "polarssl/x509_csr.h"
42 #include "polarssl/oid.h"
44 #include <string.h>
46 #if defined(POLARSSL_PEM_PARSE_C)
47 #include "polarssl/pem.h"
48 #endif
50 #if defined(POLARSSL_PLATFORM_C)
51 #include "polarssl/platform.h"
52 #else
53 #include <stdlib.h>
54 #include <stdio.h>
55 #define polarssl_free free
56 #define polarssl_malloc malloc
57 #define polarssl_snprintf snprintf
58 #endif
60 #if defined(POLARSSL_FS_IO) || defined(EFIX64) || defined(EFI32)
61 #include <stdio.h>
62 #endif
64 /* Implementation that should never be optimized out by the compiler */
65 static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
70 * Version ::= INTEGER { v1(0) }
72 static int x509_csr_get_version( unsigned char **p,
73 const unsigned char *end,
74 int *ver )
76 int ret;
78 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
80 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
82 *ver = 0;
83 return( 0 );
86 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
89 return( 0 );
93 * Parse a CSR in DER format
95 int x509_csr_parse_der( x509_csr *csr,
96 const unsigned char *buf, size_t buflen )
98 int ret;
99 size_t len;
100 unsigned char *p, *end;
101 x509_buf sig_params;
103 memset( &sig_params, 0, sizeof( x509_buf ) );
106 * Check for valid input
108 if( csr == NULL || buf == NULL )
109 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
111 x509_csr_init( csr );
114 * first copy the raw DER data
116 p = polarssl_malloc( len = buflen );
118 if( p == NULL )
119 return( POLARSSL_ERR_X509_MALLOC_FAILED );
121 memcpy( p, buf, buflen );
123 csr->raw.p = p;
124 csr->raw.len = len;
125 end = p + len;
128 * CertificationRequest ::= SEQUENCE {
129 * certificationRequestInfo CertificationRequestInfo,
130 * signatureAlgorithm AlgorithmIdentifier,
131 * signature BIT STRING
134 if( ( ret = asn1_get_tag( &p, end, &len,
135 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
137 x509_csr_free( csr );
138 return( POLARSSL_ERR_X509_INVALID_FORMAT );
141 if( len != (size_t) ( end - p ) )
143 x509_csr_free( csr );
144 return( POLARSSL_ERR_X509_INVALID_FORMAT +
145 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
149 * CertificationRequestInfo ::= SEQUENCE {
151 csr->cri.p = p;
153 if( ( ret = asn1_get_tag( &p, end, &len,
154 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
156 x509_csr_free( csr );
157 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
160 end = p + len;
161 csr->cri.len = end - csr->cri.p;
164 * Version ::= INTEGER { v1(0) }
166 if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 )
168 x509_csr_free( csr );
169 return( ret );
172 csr->version++;
174 if( csr->version != 1 )
176 x509_csr_free( csr );
177 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
181 * subject Name
183 csr->subject_raw.p = p;
185 if( ( ret = asn1_get_tag( &p, end, &len,
186 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
188 x509_csr_free( csr );
189 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
192 if( ( ret = x509_get_name( &p, p + len, &csr->subject ) ) != 0 )
194 x509_csr_free( csr );
195 return( ret );
198 csr->subject_raw.len = p - csr->subject_raw.p;
201 * subjectPKInfo SubjectPublicKeyInfo
203 if( ( ret = pk_parse_subpubkey( &p, end, &csr->pk ) ) != 0 )
205 x509_csr_free( csr );
206 return( ret );
210 * attributes [0] Attributes
212 if( ( ret = asn1_get_tag( &p, end, &len,
213 ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC ) ) != 0 )
215 x509_csr_free( csr );
216 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
218 // TODO Parse Attributes / extension requests
220 p += len;
222 end = csr->raw.p + csr->raw.len;
225 * signatureAlgorithm AlgorithmIdentifier,
226 * signature BIT STRING
228 if( ( ret = x509_get_alg( &p, end, &csr->sig_oid, &sig_params ) ) != 0 )
230 x509_csr_free( csr );
231 return( ret );
234 if( ( ret = x509_get_sig_alg( &csr->sig_oid, &sig_params,
235 &csr->sig_md, &csr->sig_pk,
236 &csr->sig_opts ) ) != 0 )
238 x509_csr_free( csr );
239 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG );
242 if( ( ret = x509_get_sig( &p, end, &csr->sig ) ) != 0 )
244 x509_csr_free( csr );
245 return( ret );
248 if( p != end )
250 x509_csr_free( csr );
251 return( POLARSSL_ERR_X509_INVALID_FORMAT +
252 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
255 return( 0 );
259 * Parse a CSR, allowing for PEM or raw DER encoding
261 int x509_csr_parse( x509_csr *csr, const unsigned char *buf, size_t buflen )
263 int ret;
264 #if defined(POLARSSL_PEM_PARSE_C)
265 size_t use_len;
266 pem_context pem;
267 #endif
270 * Check for valid input
272 if( csr == NULL || buf == NULL )
273 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
275 #if defined(POLARSSL_PEM_PARSE_C)
276 pem_init( &pem );
277 ret = pem_read_buffer( &pem,
278 "-----BEGIN CERTIFICATE REQUEST-----",
279 "-----END CERTIFICATE REQUEST-----",
280 buf, NULL, 0, &use_len );
282 if( ret == 0 )
285 * Was PEM encoded, parse the result
287 if( ( ret = x509_csr_parse_der( csr, pem.buf, pem.buflen ) ) != 0 )
288 return( ret );
290 pem_free( &pem );
291 return( 0 );
293 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
295 pem_free( &pem );
296 return( ret );
298 else
299 #endif /* POLARSSL_PEM_PARSE_C */
300 return( x509_csr_parse_der( csr, buf, buflen ) );
303 #if defined(POLARSSL_FS_IO)
305 * Load a CSR into the structure
307 int x509_csr_parse_file( x509_csr *csr, const char *path )
309 int ret;
310 size_t n;
311 unsigned char *buf;
313 if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 )
314 return( ret );
316 ret = x509_csr_parse( csr, buf, n );
318 polarssl_zeroize( buf, n + 1 );
319 polarssl_free( buf );
321 return( ret );
323 #endif /* POLARSSL_FS_IO */
325 #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
326 !defined(EFI32)
327 #include <stdarg.h>
329 #if !defined vsnprintf
330 #define vsnprintf _vsnprintf
331 #endif // vsnprintf
334 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
335 * Result value is not size of buffer needed, but -1 if no fit is possible.
337 * This fuction tries to 'fix' this by at least suggesting enlarging the
338 * size by 20.
340 static int compat_snprintf( char *str, size_t size, const char *format, ... )
342 va_list ap;
343 int res = -1;
345 va_start( ap, format );
347 res = vsnprintf( str, size, format, ap );
349 va_end( ap );
351 // No quick fix possible
352 if( res < 0 )
353 return( (int) size + 20 );
355 return( res );
358 #define snprintf compat_snprintf
359 #endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
361 #define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
363 #define SAFE_SNPRINTF() \
365 if( ret == -1 ) \
366 return( -1 ); \
368 if( (unsigned int) ret > n ) { \
369 p[n - 1] = '\0'; \
370 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
373 n -= (unsigned int) ret; \
374 p += (unsigned int) ret; \
377 #define BEFORE_COLON 14
378 #define BC "14"
380 * Return an informational string about the CSR.
382 int x509_csr_info( char *buf, size_t size, const char *prefix,
383 const x509_csr *csr )
385 int ret;
386 size_t n;
387 char *p;
388 char key_size_str[BEFORE_COLON];
390 p = buf;
391 n = size;
393 ret = polarssl_snprintf( p, n, "%sCSR version : %d",
394 prefix, csr->version );
395 SAFE_SNPRINTF();
397 ret = polarssl_snprintf( p, n, "\n%ssubject name : ", prefix );
398 SAFE_SNPRINTF();
399 ret = x509_dn_gets( p, n, &csr->subject );
400 SAFE_SNPRINTF();
402 ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix );
403 SAFE_SNPRINTF();
405 ret = x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
406 csr->sig_opts );
407 SAFE_SNPRINTF();
409 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
410 pk_get_name( &csr->pk ) ) ) != 0 )
412 return( ret );
415 ret = polarssl_snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
416 (int) pk_get_size( &csr->pk ) );
417 SAFE_SNPRINTF();
419 return( (int) ( size - n ) );
423 * Initialize a CSR
425 void x509_csr_init( x509_csr *csr )
427 memset( csr, 0, sizeof(x509_csr) );
431 * Unallocate all CSR data
433 void x509_csr_free( x509_csr *csr )
435 x509_name *name_cur;
436 x509_name *name_prv;
438 if( csr == NULL )
439 return;
441 pk_free( &csr->pk );
443 #if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
444 polarssl_free( csr->sig_opts );
445 #endif
447 name_cur = csr->subject.next;
448 while( name_cur != NULL )
450 name_prv = name_cur;
451 name_cur = name_cur->next;
452 polarssl_zeroize( name_prv, sizeof( x509_name ) );
453 polarssl_free( name_prv );
456 if( csr->raw.p != NULL )
458 polarssl_zeroize( csr->raw.p, csr->raw.len );
459 polarssl_free( csr->raw.p );
462 polarssl_zeroize( csr, sizeof( x509_csr ) );
465 #endif /* POLARSSL_X509_CSR_PARSE_C */