1 // SPDX-License-Identifier: Apache-2.0 2 /* 3 * X.509 Certificate Signing Request (CSR) parsing 4 * 5 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 6 * 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 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 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. 18 * 19 * This file is part of mbed TLS (https://tls.mbed.org) 20 */ 21 /* 22 * The ITU-T X.509 standard defines a certificate format for PKI. 23 * 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) 27 * 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 30 */ 31 32 #if !defined(MBEDTLS_CONFIG_FILE) 33 #include "mbedtls/config.h" 34 #else 35 #include MBEDTLS_CONFIG_FILE 36 #endif 37 38 #if defined(MBEDTLS_X509_CSR_PARSE_C) 39 40 #include "mbedtls/x509_csr.h" 41 #include "mbedtls/oid.h" 42 #include "mbedtls/platform_util.h" 43 44 #include <string.h> 45 46 #if defined(MBEDTLS_PEM_PARSE_C) 47 #include "mbedtls/pem.h" 48 #endif 49 50 #if defined(MBEDTLS_PLATFORM_C) 51 #include "mbedtls/platform.h" 52 #else 53 #include <stdlib.h> 54 #include <stdio.h> 55 #define mbedtls_free free 56 #define mbedtls_calloc calloc 57 #define mbedtls_snprintf snprintf 58 #endif 59 60 #if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32) 61 #include <stdio.h> 62 #endif 63 64 /* 65 * Version ::= INTEGER { v1(0) } 66 */ 67 static int x509_csr_get_version( unsigned char **p, 68 const unsigned char *end, 69 int *ver ) 70 { 71 int ret; 72 73 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 ) 74 { 75 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) 76 { 77 *ver = 0; 78 return( 0 ); 79 } 80 81 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret ); 82 } 83 84 return( 0 ); 85 } 86 87 /* 88 * Parse a CSR in DER format 89 */ 90 int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr, 91 const unsigned char *buf, size_t buflen ) 92 { 93 int ret; 94 size_t len; 95 unsigned char *p, *end; 96 mbedtls_x509_buf sig_params; 97 98 memset( &sig_params, 0, sizeof( mbedtls_x509_buf ) ); 99 100 /* 101 * Check for valid input 102 */ 103 if( csr == NULL || buf == NULL || buflen == 0 ) 104 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); 105 106 mbedtls_x509_csr_init( csr ); 107 108 /* 109 * first copy the raw DER data 110 */ 111 p = mbedtls_calloc( 1, len = buflen ); 112 113 if( p == NULL ) 114 return( MBEDTLS_ERR_X509_ALLOC_FAILED ); 115 116 memcpy( p, buf, buflen ); 117 118 csr->raw.p = p; 119 csr->raw.len = len; 120 end = p + len; 121 122 /* 123 * CertificationRequest ::= SEQUENCE { 124 * certificationRequestInfo CertificationRequestInfo, 125 * signatureAlgorithm AlgorithmIdentifier, 126 * signature BIT STRING 127 * } 128 */ 129 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, 130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 131 { 132 mbedtls_x509_csr_free( csr ); 133 return( MBEDTLS_ERR_X509_INVALID_FORMAT ); 134 } 135 136 if( len != (size_t) ( end - p ) ) 137 { 138 mbedtls_x509_csr_free( csr ); 139 return( MBEDTLS_ERR_X509_INVALID_FORMAT + 140 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); 141 } 142 143 /* 144 * CertificationRequestInfo ::= SEQUENCE { 145 */ 146 csr->cri.p = p; 147 148 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, 149 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 150 { 151 mbedtls_x509_csr_free( csr ); 152 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret ); 153 } 154 155 end = p + len; 156 csr->cri.len = end - csr->cri.p; 157 158 /* 159 * Version ::= INTEGER { v1(0) } 160 */ 161 if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 ) 162 { 163 mbedtls_x509_csr_free( csr ); 164 return( ret ); 165 } 166 167 if( csr->version != 0 ) 168 { 169 mbedtls_x509_csr_free( csr ); 170 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION ); 171 } 172 173 csr->version++; 174 175 /* 176 * subject Name 177 */ 178 csr->subject_raw.p = p; 179 180 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, 181 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 182 { 183 mbedtls_x509_csr_free( csr ); 184 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret ); 185 } 186 187 if( ( ret = mbedtls_x509_get_name( &p, p + len, &csr->subject ) ) != 0 ) 188 { 189 mbedtls_x509_csr_free( csr ); 190 return( ret ); 191 } 192 193 csr->subject_raw.len = p - csr->subject_raw.p; 194 195 /* 196 * subjectPKInfo SubjectPublicKeyInfo 197 */ 198 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &csr->pk ) ) != 0 ) 199 { 200 mbedtls_x509_csr_free( csr ); 201 return( ret ); 202 } 203 204 /* 205 * attributes [0] Attributes 206 * 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. 213 */ 214 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, 215 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 ) 216 { 217 mbedtls_x509_csr_free( csr ); 218 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret ); 219 } 220 221 p += len; 222 223 end = csr->raw.p + csr->raw.len; 224 225 /* 226 * signatureAlgorithm AlgorithmIdentifier, 227 * signature BIT STRING 228 */ 229 if( ( ret = mbedtls_x509_get_alg( &p, end, &csr->sig_oid, &sig_params ) ) != 0 ) 230 { 231 mbedtls_x509_csr_free( csr ); 232 return( ret ); 233 } 234 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 ) 238 { 239 mbedtls_x509_csr_free( csr ); 240 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG ); 241 } 242 243 if( ( ret = mbedtls_x509_get_sig( &p, end, &csr->sig ) ) != 0 ) 244 { 245 mbedtls_x509_csr_free( csr ); 246 return( ret ); 247 } 248 249 if( p != end ) 250 { 251 mbedtls_x509_csr_free( csr ); 252 return( MBEDTLS_ERR_X509_INVALID_FORMAT + 253 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); 254 } 255 256 return( 0 ); 257 } 258 259 /* 260 * Parse a CSR, allowing for PEM or raw DER encoding 261 */ 262 int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen ) 263 { 264 #if defined(MBEDTLS_PEM_PARSE_C) 265 int ret; 266 size_t use_len; 267 mbedtls_pem_context pem; 268 #endif 269 270 /* 271 * Check for valid input 272 */ 273 if( csr == NULL || buf == NULL || buflen == 0 ) 274 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); 275 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' ) 279 { 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 ) 286 { 287 ret = mbedtls_pem_read_buffer( &pem, 288 "-----BEGIN NEW CERTIFICATE REQUEST-----", 289 "-----END NEW CERTIFICATE REQUEST-----", 290 buf, NULL, 0, &use_len ); 291 } 292 293 if( ret == 0 ) 294 { 295 /* 296 * Was PEM encoded, parse the result 297 */ 298 ret = mbedtls_x509_csr_parse_der( csr, pem.buf, pem.buflen ); 299 } 300 301 mbedtls_pem_free( &pem ); 302 if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) 303 return( ret ); 304 } 305 #endif /* MBEDTLS_PEM_PARSE_C */ 306 return( mbedtls_x509_csr_parse_der( csr, buf, buflen ) ); 307 } 308 309 #if defined(MBEDTLS_FS_IO) 310 /* 311 * Load a CSR into the structure 312 */ 313 int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path ) 314 { 315 int ret; 316 size_t n; 317 unsigned char *buf; 318 319 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 ) 320 return( ret ); 321 322 ret = mbedtls_x509_csr_parse( csr, buf, n ); 323 324 mbedtls_platform_zeroize( buf, n ); 325 mbedtls_free( buf ); 326 327 return( ret ); 328 } 329 #endif /* MBEDTLS_FS_IO */ 330 331 #define BEFORE_COLON 14 332 #define BC "14" 333 /* 334 * Return an informational string about the CSR. 335 */ 336 int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix, 337 const mbedtls_x509_csr *csr ) 338 { 339 int ret; 340 size_t n; 341 char *p; 342 char key_size_str[BEFORE_COLON]; 343 344 p = buf; 345 n = size; 346 347 ret = mbedtls_snprintf( p, n, "%sCSR version : %d", 348 prefix, csr->version ); 349 MBEDTLS_X509_SAFE_SNPRINTF; 350 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; 355 356 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix ); 357 MBEDTLS_X509_SAFE_SNPRINTF; 358 359 ret = mbedtls_x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md, 360 csr->sig_opts ); 361 MBEDTLS_X509_SAFE_SNPRINTF; 362 363 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON, 364 mbedtls_pk_get_name( &csr->pk ) ) ) != 0 ) 365 { 366 return( ret ); 367 } 368 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; 372 373 return( (int) ( size - n ) ); 374 } 375 376 /* 377 * Initialize a CSR 378 */ 379 void mbedtls_x509_csr_init( mbedtls_x509_csr *csr ) 380 { 381 memset( csr, 0, sizeof(mbedtls_x509_csr) ); 382 } 383 384 /* 385 * Unallocate all CSR data 386 */ 387 void mbedtls_x509_csr_free( mbedtls_x509_csr *csr ) 388 { 389 mbedtls_x509_name *name_cur; 390 mbedtls_x509_name *name_prv; 391 392 if( csr == NULL ) 393 return; 394 395 mbedtls_pk_free( &csr->pk ); 396 397 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) 398 mbedtls_free( csr->sig_opts ); 399 #endif 400 401 name_cur = csr->subject.next; 402 while( name_cur != NULL ) 403 { 404 name_prv = name_cur; 405 name_cur = name_cur->next; 406 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) ); 407 mbedtls_free( name_prv ); 408 } 409 410 if( csr->raw.p != NULL ) 411 { 412 mbedtls_platform_zeroize( csr->raw.p, csr->raw.len ); 413 mbedtls_free( csr->raw.p ); 414 } 415 416 mbedtls_platform_zeroize( csr, sizeof( mbedtls_x509_csr ) ); 417 } 418 419 #endif /* MBEDTLS_X509_CSR_PARSE_C */ 420