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