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