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"
42 #include "mbedtls/platform_util.h"
46 #if defined(MBEDTLS_PEM_PARSE_C)
47 #include "mbedtls/pem.h"
50 #if defined(MBEDTLS_PLATFORM_C)
51 #include "mbedtls/platform.h"
55 #define mbedtls_free free
56 #define mbedtls_calloc calloc
57 #define mbedtls_snprintf snprintf
60 #if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
65 * Version ::= INTEGER { v1(0) }
67 static int x509_csr_get_version( unsigned char **p,
68 const unsigned char *end,
73 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
75 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
81 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
88 * Parse a CSR in DER format
90 int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
91 const unsigned char *buf, size_t buflen )
95 unsigned char *p, *end;
96 mbedtls_x509_buf sig_params;
98 memset( &sig_params, 0, sizeof( mbedtls_x509_buf ) );
101 * Check for valid input
103 if( csr == NULL || buf == NULL || buflen == 0 )
104 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
106 mbedtls_x509_csr_init( csr );
109 * first copy the raw DER data
111 p = mbedtls_calloc( 1, len = buflen );
114 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
116 memcpy( p, buf, buflen );
123 * CertificationRequest ::= SEQUENCE {
124 * certificationRequestInfo CertificationRequestInfo,
125 * signatureAlgorithm AlgorithmIdentifier,
126 * signature BIT STRING
129 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
132 mbedtls_x509_csr_free( csr );
133 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
136 if( len != (size_t) ( end - p ) )
138 mbedtls_x509_csr_free( csr );
139 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
140 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
144 * CertificationRequestInfo ::= SEQUENCE {
148 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
149 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
151 mbedtls_x509_csr_free( csr );
152 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
156 csr->cri.len = end - csr->cri.p;
159 * Version ::= INTEGER { v1(0) }
161 if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 )
163 mbedtls_x509_csr_free( csr );
167 if( csr->version != 0 )
169 mbedtls_x509_csr_free( csr );
170 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
178 csr->subject_raw.p = p;
180 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
181 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
183 mbedtls_x509_csr_free( csr );
184 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
187 if( ( ret = mbedtls_x509_get_name( &p, p + len, &csr->subject ) ) != 0 )
189 mbedtls_x509_csr_free( csr );
193 csr->subject_raw.len = p - csr->subject_raw.p;
196 * subjectPKInfo SubjectPublicKeyInfo
198 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &csr->pk ) ) != 0 )
200 mbedtls_x509_csr_free( csr );
205 * attributes [0] Attributes
207 * The list of possible attributes is open-ended, though RFC 2985
208 * (PKCS#9) defines a few in section 5.4. We currently don't support any,
209 * so we just ignore them. This is a safe thing to do as the worst thing
210 * that could happen is that we issue a certificate that does not match
211 * the requester's expectations - this cannot cause a violation of our
212 * signature policies.
214 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
215 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
217 mbedtls_x509_csr_free( csr );
218 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
223 end = csr->raw.p + csr->raw.len;
226 * signatureAlgorithm AlgorithmIdentifier,
227 * signature BIT STRING
229 if( ( ret = mbedtls_x509_get_alg( &p, end, &csr->sig_oid, &sig_params ) ) != 0 )
231 mbedtls_x509_csr_free( csr );
235 if( ( ret = mbedtls_x509_get_sig_alg( &csr->sig_oid, &sig_params,
236 &csr->sig_md, &csr->sig_pk,
237 &csr->sig_opts ) ) != 0 )
239 mbedtls_x509_csr_free( csr );
240 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
243 if( ( ret = mbedtls_x509_get_sig( &p, end, &csr->sig ) ) != 0 )
245 mbedtls_x509_csr_free( csr );
251 mbedtls_x509_csr_free( csr );
252 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
253 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
260 * Parse a CSR, allowing for PEM or raw DER encoding
262 int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen )
264 #if defined(MBEDTLS_PEM_PARSE_C)
267 mbedtls_pem_context pem;
271 * Check for valid input
273 if( csr == NULL || buf == NULL || buflen == 0 )
274 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
276 #if defined(MBEDTLS_PEM_PARSE_C)
277 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
278 if( buf[buflen - 1] == '\0' )
280 mbedtls_pem_init( &pem );
281 ret = mbedtls_pem_read_buffer( &pem,
282 "-----BEGIN CERTIFICATE REQUEST-----",
283 "-----END CERTIFICATE REQUEST-----",
284 buf, NULL, 0, &use_len );
285 if( ret == MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
287 ret = mbedtls_pem_read_buffer( &pem,
288 "-----BEGIN NEW CERTIFICATE REQUEST-----",
289 "-----END NEW CERTIFICATE REQUEST-----",
290 buf, NULL, 0, &use_len );
296 * Was PEM encoded, parse the result
298 ret = mbedtls_x509_csr_parse_der( csr, pem.buf, pem.buflen );
301 mbedtls_pem_free( &pem );
302 if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
305 #endif /* MBEDTLS_PEM_PARSE_C */
306 return( mbedtls_x509_csr_parse_der( csr, buf, buflen ) );
309 #if defined(MBEDTLS_FS_IO)
311 * Load a CSR into the structure
313 int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path )
319 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
322 ret = mbedtls_x509_csr_parse( csr, buf, n );
324 mbedtls_platform_zeroize( buf, n );
329 #endif /* MBEDTLS_FS_IO */
331 #define BEFORE_COLON 14
334 * Return an informational string about the CSR.
336 int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
337 const mbedtls_x509_csr *csr )
342 char key_size_str[BEFORE_COLON];
347 ret = mbedtls_snprintf( p, n, "%sCSR version : %d",
348 prefix, csr->version );
349 MBEDTLS_X509_SAFE_SNPRINTF;
351 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
352 MBEDTLS_X509_SAFE_SNPRINTF;
353 ret = mbedtls_x509_dn_gets( p, n, &csr->subject );
354 MBEDTLS_X509_SAFE_SNPRINTF;
356 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
357 MBEDTLS_X509_SAFE_SNPRINTF;
359 ret = mbedtls_x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
361 MBEDTLS_X509_SAFE_SNPRINTF;
363 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
364 mbedtls_pk_get_name( &csr->pk ) ) ) != 0 )
369 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
370 (int) mbedtls_pk_get_bitlen( &csr->pk ) );
371 MBEDTLS_X509_SAFE_SNPRINTF;
373 return( (int) ( size - n ) );
379 void mbedtls_x509_csr_init( mbedtls_x509_csr *csr )
381 memset( csr, 0, sizeof(mbedtls_x509_csr) );
385 * Unallocate all CSR data
387 void mbedtls_x509_csr_free( mbedtls_x509_csr *csr )
389 mbedtls_x509_name *name_cur;
390 mbedtls_x509_name *name_prv;
395 mbedtls_pk_free( &csr->pk );
397 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
398 mbedtls_free( csr->sig_opts );
401 name_cur = csr->subject.next;
402 while( name_cur != NULL )
405 name_cur = name_cur->next;
406 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
407 mbedtls_free( name_prv );
410 if( csr->raw.p != NULL )
412 mbedtls_platform_zeroize( csr->raw.p, csr->raw.len );
413 mbedtls_free( csr->raw.p );
416 mbedtls_platform_zeroize( csr, sizeof( mbedtls_x509_csr ) );
419 #endif /* MBEDTLS_X509_CSR_PARSE_C */