Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Simon Cooksey 0:fb7af294d5d9 1 /*
Simon Cooksey 0:fb7af294d5d9 2 * X.509 Certificate Signing Request (CSR) parsing
Simon Cooksey 0:fb7af294d5d9 3 *
Simon Cooksey 0:fb7af294d5d9 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Simon Cooksey 0:fb7af294d5d9 5 * SPDX-License-Identifier: Apache-2.0
Simon Cooksey 0:fb7af294d5d9 6 *
Simon Cooksey 0:fb7af294d5d9 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Simon Cooksey 0:fb7af294d5d9 8 * not use this file except in compliance with the License.
Simon Cooksey 0:fb7af294d5d9 9 * You may obtain a copy of the License at
Simon Cooksey 0:fb7af294d5d9 10 *
Simon Cooksey 0:fb7af294d5d9 11 * http://www.apache.org/licenses/LICENSE-2.0
Simon Cooksey 0:fb7af294d5d9 12 *
Simon Cooksey 0:fb7af294d5d9 13 * Unless required by applicable law or agreed to in writing, software
Simon Cooksey 0:fb7af294d5d9 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Simon Cooksey 0:fb7af294d5d9 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Simon Cooksey 0:fb7af294d5d9 16 * See the License for the specific language governing permissions and
Simon Cooksey 0:fb7af294d5d9 17 * limitations under the License.
Simon Cooksey 0:fb7af294d5d9 18 *
Simon Cooksey 0:fb7af294d5d9 19 * This file is part of mbed TLS (https://tls.mbed.org)
Simon Cooksey 0:fb7af294d5d9 20 */
Simon Cooksey 0:fb7af294d5d9 21 /*
Simon Cooksey 0:fb7af294d5d9 22 * The ITU-T X.509 standard defines a certificate format for PKI.
Simon Cooksey 0:fb7af294d5d9 23 *
Simon Cooksey 0:fb7af294d5d9 24 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
Simon Cooksey 0:fb7af294d5d9 25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
Simon Cooksey 0:fb7af294d5d9 26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Simon Cooksey 0:fb7af294d5d9 27 *
Simon Cooksey 0:fb7af294d5d9 28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
Simon Cooksey 0:fb7af294d5d9 29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Simon Cooksey 0:fb7af294d5d9 30 */
Simon Cooksey 0:fb7af294d5d9 31
Simon Cooksey 0:fb7af294d5d9 32 #if !defined(MBEDTLS_CONFIG_FILE)
Simon Cooksey 0:fb7af294d5d9 33 #include "mbedtls/config.h"
Simon Cooksey 0:fb7af294d5d9 34 #else
Simon Cooksey 0:fb7af294d5d9 35 #include MBEDTLS_CONFIG_FILE
Simon Cooksey 0:fb7af294d5d9 36 #endif
Simon Cooksey 0:fb7af294d5d9 37
Simon Cooksey 0:fb7af294d5d9 38 #if defined(MBEDTLS_X509_CSR_PARSE_C)
Simon Cooksey 0:fb7af294d5d9 39
Simon Cooksey 0:fb7af294d5d9 40 #include "mbedtls/x509_csr.h"
Simon Cooksey 0:fb7af294d5d9 41 #include "mbedtls/oid.h"
Simon Cooksey 0:fb7af294d5d9 42
Simon Cooksey 0:fb7af294d5d9 43 #include <string.h>
Simon Cooksey 0:fb7af294d5d9 44
Simon Cooksey 0:fb7af294d5d9 45 #if defined(MBEDTLS_PEM_PARSE_C)
Simon Cooksey 0:fb7af294d5d9 46 #include "mbedtls/pem.h"
Simon Cooksey 0:fb7af294d5d9 47 #endif
Simon Cooksey 0:fb7af294d5d9 48
Simon Cooksey 0:fb7af294d5d9 49 #if defined(MBEDTLS_PLATFORM_C)
Simon Cooksey 0:fb7af294d5d9 50 #include "mbedtls/platform.h"
Simon Cooksey 0:fb7af294d5d9 51 #else
Simon Cooksey 0:fb7af294d5d9 52 #include <stdlib.h>
Simon Cooksey 0:fb7af294d5d9 53 #include <stdio.h>
Simon Cooksey 0:fb7af294d5d9 54 #define mbedtls_free free
Simon Cooksey 0:fb7af294d5d9 55 #define mbedtls_calloc calloc
Simon Cooksey 0:fb7af294d5d9 56 #define mbedtls_snprintf snprintf
Simon Cooksey 0:fb7af294d5d9 57 #endif
Simon Cooksey 0:fb7af294d5d9 58
Simon Cooksey 0:fb7af294d5d9 59 #if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
Simon Cooksey 0:fb7af294d5d9 60 #include <stdio.h>
Simon Cooksey 0:fb7af294d5d9 61 #endif
Simon Cooksey 0:fb7af294d5d9 62
Simon Cooksey 0:fb7af294d5d9 63 /* Implementation that should never be optimized out by the compiler */
Simon Cooksey 0:fb7af294d5d9 64 static void mbedtls_zeroize( void *v, size_t n ) {
Simon Cooksey 0:fb7af294d5d9 65 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
Simon Cooksey 0:fb7af294d5d9 66 }
Simon Cooksey 0:fb7af294d5d9 67
Simon Cooksey 0:fb7af294d5d9 68 /*
Simon Cooksey 0:fb7af294d5d9 69 * Version ::= INTEGER { v1(0) }
Simon Cooksey 0:fb7af294d5d9 70 */
Simon Cooksey 0:fb7af294d5d9 71 static int x509_csr_get_version( unsigned char **p,
Simon Cooksey 0:fb7af294d5d9 72 const unsigned char *end,
Simon Cooksey 0:fb7af294d5d9 73 int *ver )
Simon Cooksey 0:fb7af294d5d9 74 {
Simon Cooksey 0:fb7af294d5d9 75 int ret;
Simon Cooksey 0:fb7af294d5d9 76
Simon Cooksey 0:fb7af294d5d9 77 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 78 {
Simon Cooksey 0:fb7af294d5d9 79 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Simon Cooksey 0:fb7af294d5d9 80 {
Simon Cooksey 0:fb7af294d5d9 81 *ver = 0;
Simon Cooksey 0:fb7af294d5d9 82 return( 0 );
Simon Cooksey 0:fb7af294d5d9 83 }
Simon Cooksey 0:fb7af294d5d9 84
Simon Cooksey 0:fb7af294d5d9 85 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Simon Cooksey 0:fb7af294d5d9 86 }
Simon Cooksey 0:fb7af294d5d9 87
Simon Cooksey 0:fb7af294d5d9 88 return( 0 );
Simon Cooksey 0:fb7af294d5d9 89 }
Simon Cooksey 0:fb7af294d5d9 90
Simon Cooksey 0:fb7af294d5d9 91 /*
Simon Cooksey 0:fb7af294d5d9 92 * Parse a CSR in DER format
Simon Cooksey 0:fb7af294d5d9 93 */
Simon Cooksey 0:fb7af294d5d9 94 int mbedtls_x509_csr_parse_der( mbedtls_x509_csr *csr,
Simon Cooksey 0:fb7af294d5d9 95 const unsigned char *buf, size_t buflen )
Simon Cooksey 0:fb7af294d5d9 96 {
Simon Cooksey 0:fb7af294d5d9 97 int ret;
Simon Cooksey 0:fb7af294d5d9 98 size_t len;
Simon Cooksey 0:fb7af294d5d9 99 unsigned char *p, *end;
Simon Cooksey 0:fb7af294d5d9 100 mbedtls_x509_buf sig_params;
Simon Cooksey 0:fb7af294d5d9 101
Simon Cooksey 0:fb7af294d5d9 102 memset( &sig_params, 0, sizeof( mbedtls_x509_buf ) );
Simon Cooksey 0:fb7af294d5d9 103
Simon Cooksey 0:fb7af294d5d9 104 /*
Simon Cooksey 0:fb7af294d5d9 105 * Check for valid input
Simon Cooksey 0:fb7af294d5d9 106 */
Simon Cooksey 0:fb7af294d5d9 107 if( csr == NULL || buf == NULL || buflen == 0 )
Simon Cooksey 0:fb7af294d5d9 108 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 109
Simon Cooksey 0:fb7af294d5d9 110 mbedtls_x509_csr_init( csr );
Simon Cooksey 0:fb7af294d5d9 111
Simon Cooksey 0:fb7af294d5d9 112 /*
Simon Cooksey 0:fb7af294d5d9 113 * first copy the raw DER data
Simon Cooksey 0:fb7af294d5d9 114 */
Simon Cooksey 0:fb7af294d5d9 115 p = mbedtls_calloc( 1, len = buflen );
Simon Cooksey 0:fb7af294d5d9 116
Simon Cooksey 0:fb7af294d5d9 117 if( p == NULL )
Simon Cooksey 0:fb7af294d5d9 118 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Simon Cooksey 0:fb7af294d5d9 119
Simon Cooksey 0:fb7af294d5d9 120 memcpy( p, buf, buflen );
Simon Cooksey 0:fb7af294d5d9 121
Simon Cooksey 0:fb7af294d5d9 122 csr->raw.p = p;
Simon Cooksey 0:fb7af294d5d9 123 csr->raw.len = len;
Simon Cooksey 0:fb7af294d5d9 124 end = p + len;
Simon Cooksey 0:fb7af294d5d9 125
Simon Cooksey 0:fb7af294d5d9 126 /*
Simon Cooksey 0:fb7af294d5d9 127 * CertificationRequest ::= SEQUENCE {
Simon Cooksey 0:fb7af294d5d9 128 * certificationRequestInfo CertificationRequestInfo,
Simon Cooksey 0:fb7af294d5d9 129 * signatureAlgorithm AlgorithmIdentifier,
Simon Cooksey 0:fb7af294d5d9 130 * signature BIT STRING
Simon Cooksey 0:fb7af294d5d9 131 * }
Simon Cooksey 0:fb7af294d5d9 132 */
Simon Cooksey 0:fb7af294d5d9 133 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
Simon Cooksey 0:fb7af294d5d9 134 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 135 {
Simon Cooksey 0:fb7af294d5d9 136 mbedtls_x509_csr_free( csr );
Simon Cooksey 0:fb7af294d5d9 137 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Simon Cooksey 0:fb7af294d5d9 138 }
Simon Cooksey 0:fb7af294d5d9 139
Simon Cooksey 0:fb7af294d5d9 140 if( len != (size_t) ( end - p ) )
Simon Cooksey 0:fb7af294d5d9 141 {
Simon Cooksey 0:fb7af294d5d9 142 mbedtls_x509_csr_free( csr );
Simon Cooksey 0:fb7af294d5d9 143 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
Simon Cooksey 0:fb7af294d5d9 144 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Simon Cooksey 0:fb7af294d5d9 145 }
Simon Cooksey 0:fb7af294d5d9 146
Simon Cooksey 0:fb7af294d5d9 147 /*
Simon Cooksey 0:fb7af294d5d9 148 * CertificationRequestInfo ::= SEQUENCE {
Simon Cooksey 0:fb7af294d5d9 149 */
Simon Cooksey 0:fb7af294d5d9 150 csr->cri.p = p;
Simon Cooksey 0:fb7af294d5d9 151
Simon Cooksey 0:fb7af294d5d9 152 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
Simon Cooksey 0:fb7af294d5d9 153 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 154 {
Simon Cooksey 0:fb7af294d5d9 155 mbedtls_x509_csr_free( csr );
Simon Cooksey 0:fb7af294d5d9 156 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Simon Cooksey 0:fb7af294d5d9 157 }
Simon Cooksey 0:fb7af294d5d9 158
Simon Cooksey 0:fb7af294d5d9 159 end = p + len;
Simon Cooksey 0:fb7af294d5d9 160 csr->cri.len = end - csr->cri.p;
Simon Cooksey 0:fb7af294d5d9 161
Simon Cooksey 0:fb7af294d5d9 162 /*
Simon Cooksey 0:fb7af294d5d9 163 * Version ::= INTEGER { v1(0) }
Simon Cooksey 0:fb7af294d5d9 164 */
Simon Cooksey 0:fb7af294d5d9 165 if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 166 {
Simon Cooksey 0:fb7af294d5d9 167 mbedtls_x509_csr_free( csr );
Simon Cooksey 0:fb7af294d5d9 168 return( ret );
Simon Cooksey 0:fb7af294d5d9 169 }
Simon Cooksey 0:fb7af294d5d9 170
Simon Cooksey 0:fb7af294d5d9 171 csr->version++;
Simon Cooksey 0:fb7af294d5d9 172
Simon Cooksey 0:fb7af294d5d9 173 if( csr->version != 1 )
Simon Cooksey 0:fb7af294d5d9 174 {
Simon Cooksey 0:fb7af294d5d9 175 mbedtls_x509_csr_free( csr );
Simon Cooksey 0:fb7af294d5d9 176 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Simon Cooksey 0:fb7af294d5d9 177 }
Simon Cooksey 0:fb7af294d5d9 178
Simon Cooksey 0:fb7af294d5d9 179 /*
Simon Cooksey 0:fb7af294d5d9 180 * subject Name
Simon Cooksey 0:fb7af294d5d9 181 */
Simon Cooksey 0:fb7af294d5d9 182 csr->subject_raw.p = p;
Simon Cooksey 0:fb7af294d5d9 183
Simon Cooksey 0:fb7af294d5d9 184 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
Simon Cooksey 0:fb7af294d5d9 185 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 186 {
Simon Cooksey 0:fb7af294d5d9 187 mbedtls_x509_csr_free( csr );
Simon Cooksey 0:fb7af294d5d9 188 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Simon Cooksey 0:fb7af294d5d9 189 }
Simon Cooksey 0:fb7af294d5d9 190
Simon Cooksey 0:fb7af294d5d9 191 if( ( ret = mbedtls_x509_get_name( &p, p + len, &csr->subject ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 192 {
Simon Cooksey 0:fb7af294d5d9 193 mbedtls_x509_csr_free( csr );
Simon Cooksey 0:fb7af294d5d9 194 return( ret );
Simon Cooksey 0:fb7af294d5d9 195 }
Simon Cooksey 0:fb7af294d5d9 196
Simon Cooksey 0:fb7af294d5d9 197 csr->subject_raw.len = p - csr->subject_raw.p;
Simon Cooksey 0:fb7af294d5d9 198
Simon Cooksey 0:fb7af294d5d9 199 /*
Simon Cooksey 0:fb7af294d5d9 200 * subjectPKInfo SubjectPublicKeyInfo
Simon Cooksey 0:fb7af294d5d9 201 */
Simon Cooksey 0:fb7af294d5d9 202 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &csr->pk ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 203 {
Simon Cooksey 0:fb7af294d5d9 204 mbedtls_x509_csr_free( csr );
Simon Cooksey 0:fb7af294d5d9 205 return( ret );
Simon Cooksey 0:fb7af294d5d9 206 }
Simon Cooksey 0:fb7af294d5d9 207
Simon Cooksey 0:fb7af294d5d9 208 /*
Simon Cooksey 0:fb7af294d5d9 209 * attributes [0] Attributes
Simon Cooksey 0:fb7af294d5d9 210 *
Simon Cooksey 0:fb7af294d5d9 211 * The list of possible attributes is open-ended, though RFC 2985
Simon Cooksey 0:fb7af294d5d9 212 * (PKCS#9) defines a few in section 5.4. We currently don't support any,
Simon Cooksey 0:fb7af294d5d9 213 * so we just ignore them. This is a safe thing to do as the worst thing
Simon Cooksey 0:fb7af294d5d9 214 * that could happen is that we issue a certificate that does not match
Simon Cooksey 0:fb7af294d5d9 215 * the requester's expectations - this cannot cause a violation of our
Simon Cooksey 0:fb7af294d5d9 216 * signature policies.
Simon Cooksey 0:fb7af294d5d9 217 */
Simon Cooksey 0:fb7af294d5d9 218 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
Simon Cooksey 0:fb7af294d5d9 219 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 220 {
Simon Cooksey 0:fb7af294d5d9 221 mbedtls_x509_csr_free( csr );
Simon Cooksey 0:fb7af294d5d9 222 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Simon Cooksey 0:fb7af294d5d9 223 }
Simon Cooksey 0:fb7af294d5d9 224
Simon Cooksey 0:fb7af294d5d9 225 p += len;
Simon Cooksey 0:fb7af294d5d9 226
Simon Cooksey 0:fb7af294d5d9 227 end = csr->raw.p + csr->raw.len;
Simon Cooksey 0:fb7af294d5d9 228
Simon Cooksey 0:fb7af294d5d9 229 /*
Simon Cooksey 0:fb7af294d5d9 230 * signatureAlgorithm AlgorithmIdentifier,
Simon Cooksey 0:fb7af294d5d9 231 * signature BIT STRING
Simon Cooksey 0:fb7af294d5d9 232 */
Simon Cooksey 0:fb7af294d5d9 233 if( ( ret = mbedtls_x509_get_alg( &p, end, &csr->sig_oid, &sig_params ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 234 {
Simon Cooksey 0:fb7af294d5d9 235 mbedtls_x509_csr_free( csr );
Simon Cooksey 0:fb7af294d5d9 236 return( ret );
Simon Cooksey 0:fb7af294d5d9 237 }
Simon Cooksey 0:fb7af294d5d9 238
Simon Cooksey 0:fb7af294d5d9 239 if( ( ret = mbedtls_x509_get_sig_alg( &csr->sig_oid, &sig_params,
Simon Cooksey 0:fb7af294d5d9 240 &csr->sig_md, &csr->sig_pk,
Simon Cooksey 0:fb7af294d5d9 241 &csr->sig_opts ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 242 {
Simon Cooksey 0:fb7af294d5d9 243 mbedtls_x509_csr_free( csr );
Simon Cooksey 0:fb7af294d5d9 244 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG );
Simon Cooksey 0:fb7af294d5d9 245 }
Simon Cooksey 0:fb7af294d5d9 246
Simon Cooksey 0:fb7af294d5d9 247 if( ( ret = mbedtls_x509_get_sig( &p, end, &csr->sig ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 248 {
Simon Cooksey 0:fb7af294d5d9 249 mbedtls_x509_csr_free( csr );
Simon Cooksey 0:fb7af294d5d9 250 return( ret );
Simon Cooksey 0:fb7af294d5d9 251 }
Simon Cooksey 0:fb7af294d5d9 252
Simon Cooksey 0:fb7af294d5d9 253 if( p != end )
Simon Cooksey 0:fb7af294d5d9 254 {
Simon Cooksey 0:fb7af294d5d9 255 mbedtls_x509_csr_free( csr );
Simon Cooksey 0:fb7af294d5d9 256 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
Simon Cooksey 0:fb7af294d5d9 257 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Simon Cooksey 0:fb7af294d5d9 258 }
Simon Cooksey 0:fb7af294d5d9 259
Simon Cooksey 0:fb7af294d5d9 260 return( 0 );
Simon Cooksey 0:fb7af294d5d9 261 }
Simon Cooksey 0:fb7af294d5d9 262
Simon Cooksey 0:fb7af294d5d9 263 /*
Simon Cooksey 0:fb7af294d5d9 264 * Parse a CSR, allowing for PEM or raw DER encoding
Simon Cooksey 0:fb7af294d5d9 265 */
Simon Cooksey 0:fb7af294d5d9 266 int mbedtls_x509_csr_parse( mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen )
Simon Cooksey 0:fb7af294d5d9 267 {
Simon Cooksey 0:fb7af294d5d9 268 int ret;
Simon Cooksey 0:fb7af294d5d9 269 #if defined(MBEDTLS_PEM_PARSE_C)
Simon Cooksey 0:fb7af294d5d9 270 size_t use_len;
Simon Cooksey 0:fb7af294d5d9 271 mbedtls_pem_context pem;
Simon Cooksey 0:fb7af294d5d9 272 #endif
Simon Cooksey 0:fb7af294d5d9 273
Simon Cooksey 0:fb7af294d5d9 274 /*
Simon Cooksey 0:fb7af294d5d9 275 * Check for valid input
Simon Cooksey 0:fb7af294d5d9 276 */
Simon Cooksey 0:fb7af294d5d9 277 if( csr == NULL || buf == NULL || buflen == 0 )
Simon Cooksey 0:fb7af294d5d9 278 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 279
Simon Cooksey 0:fb7af294d5d9 280 #if defined(MBEDTLS_PEM_PARSE_C)
Simon Cooksey 0:fb7af294d5d9 281 mbedtls_pem_init( &pem );
Simon Cooksey 0:fb7af294d5d9 282
Simon Cooksey 0:fb7af294d5d9 283 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Simon Cooksey 0:fb7af294d5d9 284 if( buf[buflen - 1] != '\0' )
Simon Cooksey 0:fb7af294d5d9 285 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
Simon Cooksey 0:fb7af294d5d9 286 else
Simon Cooksey 0:fb7af294d5d9 287 ret = mbedtls_pem_read_buffer( &pem,
Simon Cooksey 0:fb7af294d5d9 288 "-----BEGIN CERTIFICATE REQUEST-----",
Simon Cooksey 0:fb7af294d5d9 289 "-----END CERTIFICATE REQUEST-----",
Simon Cooksey 0:fb7af294d5d9 290 buf, NULL, 0, &use_len );
Simon Cooksey 0:fb7af294d5d9 291
Simon Cooksey 0:fb7af294d5d9 292 if( ret == 0 )
Simon Cooksey 0:fb7af294d5d9 293 {
Simon Cooksey 0:fb7af294d5d9 294 /*
Simon Cooksey 0:fb7af294d5d9 295 * Was PEM encoded, parse the result
Simon Cooksey 0:fb7af294d5d9 296 */
Simon Cooksey 0:fb7af294d5d9 297 if( ( ret = mbedtls_x509_csr_parse_der( csr, pem.buf, pem.buflen ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 298 return( ret );
Simon Cooksey 0:fb7af294d5d9 299
Simon Cooksey 0:fb7af294d5d9 300 mbedtls_pem_free( &pem );
Simon Cooksey 0:fb7af294d5d9 301 return( 0 );
Simon Cooksey 0:fb7af294d5d9 302 }
Simon Cooksey 0:fb7af294d5d9 303 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Simon Cooksey 0:fb7af294d5d9 304 {
Simon Cooksey 0:fb7af294d5d9 305 mbedtls_pem_free( &pem );
Simon Cooksey 0:fb7af294d5d9 306 return( ret );
Simon Cooksey 0:fb7af294d5d9 307 }
Simon Cooksey 0:fb7af294d5d9 308 else
Simon Cooksey 0:fb7af294d5d9 309 #endif /* MBEDTLS_PEM_PARSE_C */
Simon Cooksey 0:fb7af294d5d9 310 return( mbedtls_x509_csr_parse_der( csr, buf, buflen ) );
Simon Cooksey 0:fb7af294d5d9 311 }
Simon Cooksey 0:fb7af294d5d9 312
Simon Cooksey 0:fb7af294d5d9 313 #if defined(MBEDTLS_FS_IO)
Simon Cooksey 0:fb7af294d5d9 314 /*
Simon Cooksey 0:fb7af294d5d9 315 * Load a CSR into the structure
Simon Cooksey 0:fb7af294d5d9 316 */
Simon Cooksey 0:fb7af294d5d9 317 int mbedtls_x509_csr_parse_file( mbedtls_x509_csr *csr, const char *path )
Simon Cooksey 0:fb7af294d5d9 318 {
Simon Cooksey 0:fb7af294d5d9 319 int ret;
Simon Cooksey 0:fb7af294d5d9 320 size_t n;
Simon Cooksey 0:fb7af294d5d9 321 unsigned char *buf;
Simon Cooksey 0:fb7af294d5d9 322
Simon Cooksey 0:fb7af294d5d9 323 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 324 return( ret );
Simon Cooksey 0:fb7af294d5d9 325
Simon Cooksey 0:fb7af294d5d9 326 ret = mbedtls_x509_csr_parse( csr, buf, n );
Simon Cooksey 0:fb7af294d5d9 327
Simon Cooksey 0:fb7af294d5d9 328 mbedtls_zeroize( buf, n );
Simon Cooksey 0:fb7af294d5d9 329 mbedtls_free( buf );
Simon Cooksey 0:fb7af294d5d9 330
Simon Cooksey 0:fb7af294d5d9 331 return( ret );
Simon Cooksey 0:fb7af294d5d9 332 }
Simon Cooksey 0:fb7af294d5d9 333 #endif /* MBEDTLS_FS_IO */
Simon Cooksey 0:fb7af294d5d9 334
Simon Cooksey 0:fb7af294d5d9 335 #define BEFORE_COLON 14
Simon Cooksey 0:fb7af294d5d9 336 #define BC "14"
Simon Cooksey 0:fb7af294d5d9 337 /*
Simon Cooksey 0:fb7af294d5d9 338 * Return an informational string about the CSR.
Simon Cooksey 0:fb7af294d5d9 339 */
Simon Cooksey 0:fb7af294d5d9 340 int mbedtls_x509_csr_info( char *buf, size_t size, const char *prefix,
Simon Cooksey 0:fb7af294d5d9 341 const mbedtls_x509_csr *csr )
Simon Cooksey 0:fb7af294d5d9 342 {
Simon Cooksey 0:fb7af294d5d9 343 int ret;
Simon Cooksey 0:fb7af294d5d9 344 size_t n;
Simon Cooksey 0:fb7af294d5d9 345 char *p;
Simon Cooksey 0:fb7af294d5d9 346 char key_size_str[BEFORE_COLON];
Simon Cooksey 0:fb7af294d5d9 347
Simon Cooksey 0:fb7af294d5d9 348 p = buf;
Simon Cooksey 0:fb7af294d5d9 349 n = size;
Simon Cooksey 0:fb7af294d5d9 350
Simon Cooksey 0:fb7af294d5d9 351 ret = mbedtls_snprintf( p, n, "%sCSR version : %d",
Simon Cooksey 0:fb7af294d5d9 352 prefix, csr->version );
Simon Cooksey 0:fb7af294d5d9 353 MBEDTLS_X509_SAFE_SNPRINTF;
Simon Cooksey 0:fb7af294d5d9 354
Simon Cooksey 0:fb7af294d5d9 355 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Simon Cooksey 0:fb7af294d5d9 356 MBEDTLS_X509_SAFE_SNPRINTF;
Simon Cooksey 0:fb7af294d5d9 357 ret = mbedtls_x509_dn_gets( p, n, &csr->subject );
Simon Cooksey 0:fb7af294d5d9 358 MBEDTLS_X509_SAFE_SNPRINTF;
Simon Cooksey 0:fb7af294d5d9 359
Simon Cooksey 0:fb7af294d5d9 360 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Simon Cooksey 0:fb7af294d5d9 361 MBEDTLS_X509_SAFE_SNPRINTF;
Simon Cooksey 0:fb7af294d5d9 362
Simon Cooksey 0:fb7af294d5d9 363 ret = mbedtls_x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
Simon Cooksey 0:fb7af294d5d9 364 csr->sig_opts );
Simon Cooksey 0:fb7af294d5d9 365 MBEDTLS_X509_SAFE_SNPRINTF;
Simon Cooksey 0:fb7af294d5d9 366
Simon Cooksey 0:fb7af294d5d9 367 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
Simon Cooksey 0:fb7af294d5d9 368 mbedtls_pk_get_name( &csr->pk ) ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 369 {
Simon Cooksey 0:fb7af294d5d9 370 return( ret );
Simon Cooksey 0:fb7af294d5d9 371 }
Simon Cooksey 0:fb7af294d5d9 372
Simon Cooksey 0:fb7af294d5d9 373 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
Simon Cooksey 0:fb7af294d5d9 374 (int) mbedtls_pk_get_bitlen( &csr->pk ) );
Simon Cooksey 0:fb7af294d5d9 375 MBEDTLS_X509_SAFE_SNPRINTF;
Simon Cooksey 0:fb7af294d5d9 376
Simon Cooksey 0:fb7af294d5d9 377 return( (int) ( size - n ) );
Simon Cooksey 0:fb7af294d5d9 378 }
Simon Cooksey 0:fb7af294d5d9 379
Simon Cooksey 0:fb7af294d5d9 380 /*
Simon Cooksey 0:fb7af294d5d9 381 * Initialize a CSR
Simon Cooksey 0:fb7af294d5d9 382 */
Simon Cooksey 0:fb7af294d5d9 383 void mbedtls_x509_csr_init( mbedtls_x509_csr *csr )
Simon Cooksey 0:fb7af294d5d9 384 {
Simon Cooksey 0:fb7af294d5d9 385 memset( csr, 0, sizeof(mbedtls_x509_csr) );
Simon Cooksey 0:fb7af294d5d9 386 }
Simon Cooksey 0:fb7af294d5d9 387
Simon Cooksey 0:fb7af294d5d9 388 /*
Simon Cooksey 0:fb7af294d5d9 389 * Unallocate all CSR data
Simon Cooksey 0:fb7af294d5d9 390 */
Simon Cooksey 0:fb7af294d5d9 391 void mbedtls_x509_csr_free( mbedtls_x509_csr *csr )
Simon Cooksey 0:fb7af294d5d9 392 {
Simon Cooksey 0:fb7af294d5d9 393 mbedtls_x509_name *name_cur;
Simon Cooksey 0:fb7af294d5d9 394 mbedtls_x509_name *name_prv;
Simon Cooksey 0:fb7af294d5d9 395
Simon Cooksey 0:fb7af294d5d9 396 if( csr == NULL )
Simon Cooksey 0:fb7af294d5d9 397 return;
Simon Cooksey 0:fb7af294d5d9 398
Simon Cooksey 0:fb7af294d5d9 399 mbedtls_pk_free( &csr->pk );
Simon Cooksey 0:fb7af294d5d9 400
Simon Cooksey 0:fb7af294d5d9 401 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Simon Cooksey 0:fb7af294d5d9 402 mbedtls_free( csr->sig_opts );
Simon Cooksey 0:fb7af294d5d9 403 #endif
Simon Cooksey 0:fb7af294d5d9 404
Simon Cooksey 0:fb7af294d5d9 405 name_cur = csr->subject.next;
Simon Cooksey 0:fb7af294d5d9 406 while( name_cur != NULL )
Simon Cooksey 0:fb7af294d5d9 407 {
Simon Cooksey 0:fb7af294d5d9 408 name_prv = name_cur;
Simon Cooksey 0:fb7af294d5d9 409 name_cur = name_cur->next;
Simon Cooksey 0:fb7af294d5d9 410 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Simon Cooksey 0:fb7af294d5d9 411 mbedtls_free( name_prv );
Simon Cooksey 0:fb7af294d5d9 412 }
Simon Cooksey 0:fb7af294d5d9 413
Simon Cooksey 0:fb7af294d5d9 414 if( csr->raw.p != NULL )
Simon Cooksey 0:fb7af294d5d9 415 {
Simon Cooksey 0:fb7af294d5d9 416 mbedtls_zeroize( csr->raw.p, csr->raw.len );
Simon Cooksey 0:fb7af294d5d9 417 mbedtls_free( csr->raw.p );
Simon Cooksey 0:fb7af294d5d9 418 }
Simon Cooksey 0:fb7af294d5d9 419
Simon Cooksey 0:fb7af294d5d9 420 mbedtls_zeroize( csr, sizeof( mbedtls_x509_csr ) );
Simon Cooksey 0:fb7af294d5d9 421 }
Simon Cooksey 0:fb7af294d5d9 422
Simon Cooksey 0:fb7af294d5d9 423 #endif /* MBEDTLS_X509_CSR_PARSE_C */