Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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