2 * X.509 Certificate Signing Request (CSR) parsing
4 * Copyright The Mbed TLS Contributors
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.
20 * The ITU-T X.509 standard defines a certificate format for PKI.
22 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
32 #if defined(MBEDTLS_X509_CSR_PARSE_C)
34 #include "mbedtls/x509_csr.h"
35 #include "mbedtls/error.h"
36 #include "mbedtls/oid.h"
37 #include "mbedtls/platform_util.h"
41 #if defined(MBEDTLS_PEM_PARSE_C)
42 #include "mbedtls/pem.h"
45 #if defined(MBEDTLS_PLATFORM_C)
46 #include "mbedtls/platform.h"
50 #define mbedtls_free free
51 #define mbedtls_calloc calloc
52 #define mbedtls_snprintf snprintf
55 #if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
60 * Version ::= INTEGER { v1(0) }
62 static int x509_csr_get_version(unsigned char **p
,
63 const unsigned char *end
,
65 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
67 if ((ret
= mbedtls_asn1_get_int(p
, end
, ver
)) != 0) {
68 if (ret
== MBEDTLS_ERR_ASN1_UNEXPECTED_TAG
) {
73 return (MBEDTLS_ERR_X509_INVALID_VERSION
+ ret
);
80 * Parse a CSR in DER format
82 int mbedtls_x509_csr_parse_der(mbedtls_x509_csr
*csr
,
83 const unsigned char *buf
, size_t buflen
) {
84 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
86 unsigned char *p
, *end
;
87 mbedtls_x509_buf sig_params
;
89 memset(&sig_params
, 0, sizeof(mbedtls_x509_buf
));
92 * Check for valid input
94 if (csr
== NULL
|| buf
== NULL
|| buflen
== 0)
95 return (MBEDTLS_ERR_X509_BAD_INPUT_DATA
);
97 mbedtls_x509_csr_init(csr
);
100 * first copy the raw DER data
102 p
= mbedtls_calloc(1, len
= buflen
);
105 return (MBEDTLS_ERR_X509_ALLOC_FAILED
);
107 memcpy(p
, buf
, buflen
);
114 * CertificationRequest ::= SEQUENCE {
115 * certificationRequestInfo CertificationRequestInfo,
116 * signatureAlgorithm AlgorithmIdentifier,
117 * signature BIT STRING
120 if ((ret
= mbedtls_asn1_get_tag(&p
, end
, &len
,
121 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
)) != 0) {
122 mbedtls_x509_csr_free(csr
);
123 return (MBEDTLS_ERR_X509_INVALID_FORMAT
);
126 if (len
!= (size_t)(end
- p
)) {
127 mbedtls_x509_csr_free(csr
);
128 return (MBEDTLS_ERR_X509_INVALID_FORMAT
+
129 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
133 * CertificationRequestInfo ::= SEQUENCE {
137 if ((ret
= mbedtls_asn1_get_tag(&p
, end
, &len
,
138 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
)) != 0) {
139 mbedtls_x509_csr_free(csr
);
140 return (MBEDTLS_ERR_X509_INVALID_FORMAT
+ ret
);
144 csr
->cri
.len
= end
- csr
->cri
.p
;
147 * Version ::= INTEGER { v1(0) }
149 if ((ret
= x509_csr_get_version(&p
, end
, &csr
->version
)) != 0) {
150 mbedtls_x509_csr_free(csr
);
154 if (csr
->version
!= 0) {
155 mbedtls_x509_csr_free(csr
);
156 return (MBEDTLS_ERR_X509_UNKNOWN_VERSION
);
164 csr
->subject_raw
.p
= p
;
166 if ((ret
= mbedtls_asn1_get_tag(&p
, end
, &len
,
167 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
)) != 0) {
168 mbedtls_x509_csr_free(csr
);
169 return (MBEDTLS_ERR_X509_INVALID_FORMAT
+ ret
);
172 if ((ret
= mbedtls_x509_get_name(&p
, p
+ len
, &csr
->subject
)) != 0) {
173 mbedtls_x509_csr_free(csr
);
177 csr
->subject_raw
.len
= p
- csr
->subject_raw
.p
;
180 * subjectPKInfo SubjectPublicKeyInfo
182 if ((ret
= mbedtls_pk_parse_subpubkey(&p
, end
, &csr
->pk
)) != 0) {
183 mbedtls_x509_csr_free(csr
);
188 * attributes [0] Attributes
190 * The list of possible attributes is open-ended, though RFC 2985
191 * (PKCS#9) defines a few in section 5.4. We currently don't support any,
192 * so we just ignore them. This is a safe thing to do as the worst thing
193 * that could happen is that we issue a certificate that does not match
194 * the requester's expectations - this cannot cause a violation of our
195 * signature policies.
197 if ((ret
= mbedtls_asn1_get_tag(&p
, end
, &len
,
198 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_CONTEXT_SPECIFIC
)) != 0) {
199 mbedtls_x509_csr_free(csr
);
200 return (MBEDTLS_ERR_X509_INVALID_FORMAT
+ ret
);
205 end
= csr
->raw
.p
+ csr
->raw
.len
;
208 * signatureAlgorithm AlgorithmIdentifier,
209 * signature BIT STRING
211 if ((ret
= mbedtls_x509_get_alg(&p
, end
, &csr
->sig_oid
, &sig_params
)) != 0) {
212 mbedtls_x509_csr_free(csr
);
216 if ((ret
= mbedtls_x509_get_sig_alg(&csr
->sig_oid
, &sig_params
,
217 &csr
->sig_md
, &csr
->sig_pk
,
218 &csr
->sig_opts
)) != 0) {
219 mbedtls_x509_csr_free(csr
);
220 return (MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG
);
223 if ((ret
= mbedtls_x509_get_sig(&p
, end
, &csr
->sig
)) != 0) {
224 mbedtls_x509_csr_free(csr
);
229 mbedtls_x509_csr_free(csr
);
230 return (MBEDTLS_ERR_X509_INVALID_FORMAT
+
231 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
238 * Parse a CSR, allowing for PEM or raw DER encoding
240 int mbedtls_x509_csr_parse(mbedtls_x509_csr
*csr
, const unsigned char *buf
, size_t buflen
) {
241 #if defined(MBEDTLS_PEM_PARSE_C)
242 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
244 mbedtls_pem_context pem
;
248 * Check for valid input
250 if (csr
== NULL
|| buf
== NULL
|| buflen
== 0)
251 return (MBEDTLS_ERR_X509_BAD_INPUT_DATA
);
253 #if defined(MBEDTLS_PEM_PARSE_C)
254 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
255 if (buf
[buflen
- 1] == '\0') {
256 mbedtls_pem_init(&pem
);
257 ret
= mbedtls_pem_read_buffer(&pem
,
258 "-----BEGIN CERTIFICATE REQUEST-----",
259 "-----END CERTIFICATE REQUEST-----",
260 buf
, NULL
, 0, &use_len
);
261 if (ret
== MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT
) {
262 ret
= mbedtls_pem_read_buffer(&pem
,
263 "-----BEGIN NEW CERTIFICATE REQUEST-----",
264 "-----END NEW CERTIFICATE REQUEST-----",
265 buf
, NULL
, 0, &use_len
);
270 * Was PEM encoded, parse the result
272 ret
= mbedtls_x509_csr_parse_der(csr
, pem
.buf
, pem
.buflen
);
275 mbedtls_pem_free(&pem
);
276 if (ret
!= MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT
)
279 #endif /* MBEDTLS_PEM_PARSE_C */
280 return (mbedtls_x509_csr_parse_der(csr
, buf
, buflen
));
283 #if defined(MBEDTLS_FS_IO)
285 * Load a CSR into the structure
287 int mbedtls_x509_csr_parse_file(mbedtls_x509_csr
*csr
, const char *path
) {
288 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
292 if ((ret
= mbedtls_pk_load_file(path
, &buf
, &n
)) != 0)
295 ret
= mbedtls_x509_csr_parse(csr
, buf
, n
);
297 mbedtls_platform_zeroize(buf
, n
);
302 #endif /* MBEDTLS_FS_IO */
304 #define BEFORE_COLON 14
307 * Return an informational string about the CSR.
309 int mbedtls_x509_csr_info(char *buf
, size_t size
, const char *prefix
,
310 const mbedtls_x509_csr
*csr
) {
311 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
314 char key_size_str
[BEFORE_COLON
];
319 ret
= mbedtls_snprintf(p
, n
, "%sCSR version : %d",
320 prefix
, csr
->version
);
321 MBEDTLS_X509_SAFE_SNPRINTF
;
323 ret
= mbedtls_snprintf(p
, n
, "\n%ssubject name : ", prefix
);
324 MBEDTLS_X509_SAFE_SNPRINTF
;
325 ret
= mbedtls_x509_dn_gets(p
, n
, &csr
->subject
);
326 MBEDTLS_X509_SAFE_SNPRINTF
;
328 ret
= mbedtls_snprintf(p
, n
, "\n%ssigned using : ", prefix
);
329 MBEDTLS_X509_SAFE_SNPRINTF
;
331 ret
= mbedtls_x509_sig_alg_gets(p
, n
, &csr
->sig_oid
, csr
->sig_pk
, csr
->sig_md
,
333 MBEDTLS_X509_SAFE_SNPRINTF
;
335 if ((ret
= mbedtls_x509_key_size_helper(key_size_str
, BEFORE_COLON
,
336 mbedtls_pk_get_name(&csr
->pk
))) != 0) {
340 ret
= mbedtls_snprintf(p
, n
, "\n%s%-" BC
"s: %d bits\n", prefix
, key_size_str
,
341 (int) mbedtls_pk_get_bitlen(&csr
->pk
));
342 MBEDTLS_X509_SAFE_SNPRINTF
;
344 return ((int)(size
- n
));
350 void mbedtls_x509_csr_init(mbedtls_x509_csr
*csr
) {
351 memset(csr
, 0, sizeof(mbedtls_x509_csr
));
355 * Unallocate all CSR data
357 void mbedtls_x509_csr_free(mbedtls_x509_csr
*csr
) {
358 mbedtls_x509_name
*name_cur
;
359 mbedtls_x509_name
*name_prv
;
364 mbedtls_pk_free(&csr
->pk
);
366 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
367 mbedtls_free(csr
->sig_opts
);
370 name_cur
= csr
->subject
.next
;
371 while (name_cur
!= NULL
) {
373 name_cur
= name_cur
->next
;
374 mbedtls_platform_zeroize(name_prv
, sizeof(mbedtls_x509_name
));
375 mbedtls_free(name_prv
);
378 if (csr
->raw
.p
!= NULL
) {
379 mbedtls_platform_zeroize(csr
->raw
.p
, csr
->raw
.len
);
380 mbedtls_free(csr
->raw
.p
);
383 mbedtls_platform_zeroize(csr
, sizeof(mbedtls_x509_csr
));
386 #endif /* MBEDTLS_X509_CSR_PARSE_C */