2 * X.509 Certificate Signing Request (CSR) parsing
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 * The ITU-T X.509 standard defines a certificate format for PKI.
24 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
32 #if !defined(MBEDTLS_CONFIG_FILE)
33 #include "mbedtls/config.h"
35 #include MBEDTLS_CONFIG_FILE
38 #if defined(MBEDTLS_X509_CSR_PARSE_C)
40 #include "mbedtls/x509_csr.h"
41 #include "mbedtls/oid.h"
45 #if defined(MBEDTLS_PEM_PARSE_C)
46 #include "mbedtls/pem.h"
49 #if defined(MBEDTLS_PLATFORM_C)
50 #include "mbedtls/platform.h"
54 #define mbedtls_free free
55 #define mbedtls_calloc calloc
56 #define mbedtls_snprintf snprintf
59 #if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
63 /* Implementation that should never be optimized out by the compiler */
64 static void mbedtls_zeroize( void *v
, size_t n
) {
65 volatile unsigned char *p
= v
; while( n
-- ) *p
++ = 0;
69 * Version ::= INTEGER { v1(0) }
71 static int x509_csr_get_version( unsigned char **p
,
72 const unsigned char *end
,
77 if( ( ret
= mbedtls_asn1_get_int( p
, end
, ver
) ) != 0 )
79 if( ret
== MBEDTLS_ERR_ASN1_UNEXPECTED_TAG
)
85 return( MBEDTLS_ERR_X509_INVALID_VERSION
+ ret
);
92 * Parse a CSR in DER format
94 int mbedtls_x509_csr_parse_der( mbedtls_x509_csr
*csr
,
95 const unsigned char *buf
, size_t buflen
)
99 unsigned char *p
, *end
;
100 mbedtls_x509_buf sig_params
;
102 memset( &sig_params
, 0, sizeof( mbedtls_x509_buf
) );
105 * Check for valid input
107 if( csr
== NULL
|| buf
== NULL
)
108 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA
);
110 mbedtls_x509_csr_init( csr
);
113 * first copy the raw DER data
115 p
= mbedtls_calloc( 1, len
= buflen
);
118 return( MBEDTLS_ERR_X509_ALLOC_FAILED
);
120 memcpy( p
, buf
, buflen
);
127 * CertificationRequest ::= SEQUENCE {
128 * certificationRequestInfo CertificationRequestInfo,
129 * signatureAlgorithm AlgorithmIdentifier,
130 * signature BIT STRING
133 if( ( ret
= mbedtls_asn1_get_tag( &p
, end
, &len
,
134 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
) ) != 0 )
136 mbedtls_x509_csr_free( csr
);
137 return( MBEDTLS_ERR_X509_INVALID_FORMAT
);
140 if( len
!= (size_t) ( end
- p
) )
142 mbedtls_x509_csr_free( csr
);
143 return( MBEDTLS_ERR_X509_INVALID_FORMAT
+
144 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
148 * CertificationRequestInfo ::= SEQUENCE {
152 if( ( ret
= mbedtls_asn1_get_tag( &p
, end
, &len
,
153 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
) ) != 0 )
155 mbedtls_x509_csr_free( csr
);
156 return( MBEDTLS_ERR_X509_INVALID_FORMAT
+ ret
);
160 csr
->cri
.len
= end
- csr
->cri
.p
;
163 * Version ::= INTEGER { v1(0) }
165 if( ( ret
= x509_csr_get_version( &p
, end
, &csr
->version
) ) != 0 )
167 mbedtls_x509_csr_free( csr
);
173 if( csr
->version
!= 1 )
175 mbedtls_x509_csr_free( csr
);
176 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION
);
182 csr
->subject_raw
.p
= p
;
184 if( ( ret
= mbedtls_asn1_get_tag( &p
, end
, &len
,
185 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
) ) != 0 )
187 mbedtls_x509_csr_free( csr
);
188 return( MBEDTLS_ERR_X509_INVALID_FORMAT
+ ret
);
191 if( ( ret
= mbedtls_x509_get_name( &p
, p
+ len
, &csr
->subject
) ) != 0 )
193 mbedtls_x509_csr_free( csr
);
197 csr
->subject_raw
.len
= p
- csr
->subject_raw
.p
;
200 * subjectPKInfo SubjectPublicKeyInfo
202 if( ( ret
= mbedtls_pk_parse_subpubkey( &p
, end
, &csr
->pk
) ) != 0 )
204 mbedtls_x509_csr_free( csr
);
209 * attributes [0] Attributes
211 if( ( ret
= mbedtls_asn1_get_tag( &p
, end
, &len
,
212 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_CONTEXT_SPECIFIC
) ) != 0 )
214 mbedtls_x509_csr_free( csr
);
215 return( MBEDTLS_ERR_X509_INVALID_FORMAT
+ ret
);
217 // TODO Parse Attributes / extension requests
221 end
= csr
->raw
.p
+ csr
->raw
.len
;
224 * signatureAlgorithm AlgorithmIdentifier,
225 * signature BIT STRING
227 if( ( ret
= mbedtls_x509_get_alg( &p
, end
, &csr
->sig_oid
, &sig_params
) ) != 0 )
229 mbedtls_x509_csr_free( csr
);
233 if( ( ret
= mbedtls_x509_get_sig_alg( &csr
->sig_oid
, &sig_params
,
234 &csr
->sig_md
, &csr
->sig_pk
,
235 &csr
->sig_opts
) ) != 0 )
237 mbedtls_x509_csr_free( csr
);
238 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG
);
241 if( ( ret
= mbedtls_x509_get_sig( &p
, end
, &csr
->sig
) ) != 0 )
243 mbedtls_x509_csr_free( csr
);
249 mbedtls_x509_csr_free( csr
);
250 return( MBEDTLS_ERR_X509_INVALID_FORMAT
+
251 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
258 * Parse a CSR, allowing for PEM or raw DER encoding
260 int mbedtls_x509_csr_parse( mbedtls_x509_csr
*csr
, const unsigned char *buf
, size_t buflen
)
263 #if defined(MBEDTLS_PEM_PARSE_C)
265 mbedtls_pem_context pem
;
269 * Check for valid input
271 if( csr
== NULL
|| buf
== NULL
)
272 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA
);
274 #if defined(MBEDTLS_PEM_PARSE_C)
275 mbedtls_pem_init( &pem
);
277 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
278 if( buflen
== 0 || buf
[buflen
- 1] != '\0' )
279 ret
= MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT
;
281 ret
= mbedtls_pem_read_buffer( &pem
,
282 "-----BEGIN CERTIFICATE REQUEST-----",
283 "-----END CERTIFICATE REQUEST-----",
284 buf
, NULL
, 0, &use_len
);
289 * Was PEM encoded, parse the result
291 if( ( ret
= mbedtls_x509_csr_parse_der( csr
, pem
.buf
, pem
.buflen
) ) != 0 )
294 mbedtls_pem_free( &pem
);
297 else if( ret
!= MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT
)
299 mbedtls_pem_free( &pem
);
303 #endif /* MBEDTLS_PEM_PARSE_C */
304 return( mbedtls_x509_csr_parse_der( csr
, buf
, buflen
) );
307 #if defined(MBEDTLS_FS_IO)
309 * Load a CSR into the structure
311 int mbedtls_x509_csr_parse_file( mbedtls_x509_csr
*csr
, const char *path
)
317 if( ( ret
= mbedtls_pk_load_file( path
, &buf
, &n
) ) != 0 )
320 ret
= mbedtls_x509_csr_parse( csr
, buf
, n
);
322 mbedtls_zeroize( buf
, n
);
327 #endif /* MBEDTLS_FS_IO */
329 #define BEFORE_COLON 14
332 * Return an informational string about the CSR.
334 int mbedtls_x509_csr_info( char *buf
, size_t size
, const char *prefix
,
335 const mbedtls_x509_csr
*csr
)
340 char key_size_str
[BEFORE_COLON
];
345 ret
= mbedtls_snprintf( p
, n
, "%sCSR version : %d",
346 prefix
, csr
->version
);
347 MBEDTLS_X509_SAFE_SNPRINTF
;
349 ret
= mbedtls_snprintf( p
, n
, "\n%ssubject name : ", prefix
);
350 MBEDTLS_X509_SAFE_SNPRINTF
;
351 ret
= mbedtls_x509_dn_gets( p
, n
, &csr
->subject
);
352 MBEDTLS_X509_SAFE_SNPRINTF
;
354 ret
= mbedtls_snprintf( p
, n
, "\n%ssigned using : ", prefix
);
355 MBEDTLS_X509_SAFE_SNPRINTF
;
357 ret
= mbedtls_x509_sig_alg_gets( p
, n
, &csr
->sig_oid
, csr
->sig_pk
, csr
->sig_md
,
359 MBEDTLS_X509_SAFE_SNPRINTF
;
361 if( ( ret
= mbedtls_x509_key_size_helper( key_size_str
, BEFORE_COLON
,
362 mbedtls_pk_get_name( &csr
->pk
) ) ) != 0 )
367 ret
= mbedtls_snprintf( p
, n
, "\n%s%-" BC
"s: %d bits\n", prefix
, key_size_str
,
368 (int) mbedtls_pk_get_bitlen( &csr
->pk
) );
369 MBEDTLS_X509_SAFE_SNPRINTF
;
371 return( (int) ( size
- n
) );
377 void mbedtls_x509_csr_init( mbedtls_x509_csr
*csr
)
379 memset( csr
, 0, sizeof(mbedtls_x509_csr
) );
383 * Unallocate all CSR data
385 void mbedtls_x509_csr_free( mbedtls_x509_csr
*csr
)
387 mbedtls_x509_name
*name_cur
;
388 mbedtls_x509_name
*name_prv
;
393 mbedtls_pk_free( &csr
->pk
);
395 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
396 mbedtls_free( csr
->sig_opts
);
399 name_cur
= csr
->subject
.next
;
400 while( name_cur
!= NULL
)
403 name_cur
= name_cur
->next
;
404 mbedtls_zeroize( name_prv
, sizeof( mbedtls_x509_name
) );
405 mbedtls_free( name_prv
);
408 if( csr
->raw
.p
!= NULL
)
410 mbedtls_zeroize( csr
->raw
.p
, csr
->raw
.len
);
411 mbedtls_free( csr
->raw
.p
);
414 mbedtls_zeroize( csr
, sizeof( mbedtls_x509_csr
) );
417 #endif /* MBEDTLS_X509_CSR_PARSE_C */