mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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