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