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"
36 #include POLARSSL_CONFIG_FILE
39 #if defined(POLARSSL_X509_CSR_PARSE_C)
41 #include "polarssl/x509_csr.h"
42 #include "polarssl/oid.h"
46 #if defined(POLARSSL_PEM_PARSE_C)
47 #include "polarssl/pem.h"
50 #if defined(POLARSSL_PLATFORM_C)
51 #include "polarssl/platform.h"
55 #define polarssl_free free
56 #define polarssl_malloc malloc
57 #define polarssl_snprintf snprintf
60 #if defined(POLARSSL_FS_IO) || defined(EFIX64) || defined(EFI32)
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
,
78 if( ( ret
= asn1_get_int( p
, end
, ver
) ) != 0 )
80 if( ret
== POLARSSL_ERR_ASN1_UNEXPECTED_TAG
)
86 return( POLARSSL_ERR_X509_INVALID_VERSION
+ ret
);
93 * Parse a CSR in DER format
95 int x509_csr_parse_der( x509_csr
*csr
,
96 const unsigned char *buf
, size_t buflen
)
100 unsigned char *p
, *end
;
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
);
119 return( POLARSSL_ERR_X509_MALLOC_FAILED
);
121 memcpy( p
, buf
, buflen
);
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 {
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
);
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
);
174 if( csr
->version
!= 1 )
176 x509_csr_free( csr
);
177 return( POLARSSL_ERR_X509_UNKNOWN_VERSION
);
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
);
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
);
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
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
);
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
);
250 x509_csr_free( csr
);
251 return( POLARSSL_ERR_X509_INVALID_FORMAT
+
252 POLARSSL_ERR_ASN1_LENGTH_MISMATCH
);
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
)
264 #if defined(POLARSSL_PEM_PARSE_C)
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)
277 ret
= pem_read_buffer( &pem
,
278 "-----BEGIN CERTIFICATE REQUEST-----",
279 "-----END CERTIFICATE REQUEST-----",
280 buf
, NULL
, 0, &use_len
);
285 * Was PEM encoded, parse the result
287 if( ( ret
= x509_csr_parse_der( csr
, pem
.buf
, pem
.buflen
) ) != 0 )
293 else if( ret
!= POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT
)
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
)
313 if( ( ret
= pk_load_file( path
, &buf
, &n
) ) != 0 )
316 ret
= x509_csr_parse( csr
, buf
, n
);
318 polarssl_zeroize( buf
, n
+ 1 );
319 polarssl_free( buf
);
323 #endif /* POLARSSL_FS_IO */
325 #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
329 #if !defined vsnprintf
330 #define vsnprintf _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
340 static int compat_snprintf( char *str
, size_t size
, const char *format
, ... )
345 va_start( ap
, format
);
347 res
= vsnprintf( str
, size
, format
, ap
);
351 // No quick fix possible
353 return( (int) size
+ 20 );
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() \
368 if( (unsigned int) ret > n ) { \
370 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
373 n -= (unsigned int) ret; \
374 p += (unsigned int) ret; \
377 #define BEFORE_COLON 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
)
388 char key_size_str
[BEFORE_COLON
];
393 ret
= polarssl_snprintf( p
, n
, "%sCSR version : %d",
394 prefix
, csr
->version
);
397 ret
= polarssl_snprintf( p
, n
, "\n%ssubject name : ", prefix
);
399 ret
= x509_dn_gets( p
, n
, &csr
->subject
);
402 ret
= polarssl_snprintf( p
, n
, "\n%ssigned using : ", prefix
);
405 ret
= x509_sig_alg_gets( p
, n
, &csr
->sig_oid
, csr
->sig_pk
, csr
->sig_md
,
409 if( ( ret
= x509_key_size_helper( key_size_str
, BEFORE_COLON
,
410 pk_get_name( &csr
->pk
) ) ) != 0 )
415 ret
= polarssl_snprintf( p
, n
, "\n%s%-" BC
"s: %d bits\n", prefix
, key_size_str
,
416 (int) pk_get_size( &csr
->pk
) );
419 return( (int) ( size
- n
) );
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
)
443 #if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
444 polarssl_free( csr
->sig_opts
);
447 name_cur
= csr
->subject
.next
;
448 while( name_cur
!= NULL
)
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 */