mbed TLS library
Dependents: HTTPClient-SSL WS_SERVER
polarssl/x509_csr.c@0:137634ff4186, 2015-06-11 (annotated)
- Committer:
- ansond
- Date:
- Thu Jun 11 03:27:03 2015 +0000
- Revision:
- 0:137634ff4186
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:137634ff4186 | 1 | /* |
ansond | 0:137634ff4186 | 2 | * X.509 Certificate Signing Request (CSR) parsing |
ansond | 0:137634ff4186 | 3 | * |
ansond | 0:137634ff4186 | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
ansond | 0:137634ff4186 | 5 | * |
ansond | 0:137634ff4186 | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
ansond | 0:137634ff4186 | 7 | * |
ansond | 0:137634ff4186 | 8 | * This program is free software; you can redistribute it and/or modify |
ansond | 0:137634ff4186 | 9 | * it under the terms of the GNU General Public License as published by |
ansond | 0:137634ff4186 | 10 | * the Free Software Foundation; either version 2 of the License, or |
ansond | 0:137634ff4186 | 11 | * (at your option) any later version. |
ansond | 0:137634ff4186 | 12 | * |
ansond | 0:137634ff4186 | 13 | * This program is distributed in the hope that it will be useful, |
ansond | 0:137634ff4186 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ansond | 0:137634ff4186 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ansond | 0:137634ff4186 | 16 | * GNU General Public License for more details. |
ansond | 0:137634ff4186 | 17 | * |
ansond | 0:137634ff4186 | 18 | * You should have received a copy of the GNU General Public License along |
ansond | 0:137634ff4186 | 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
ansond | 0:137634ff4186 | 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
ansond | 0:137634ff4186 | 21 | */ |
ansond | 0:137634ff4186 | 22 | /* |
ansond | 0:137634ff4186 | 23 | * The ITU-T X.509 standard defines a certificate format for PKI. |
ansond | 0:137634ff4186 | 24 | * |
ansond | 0:137634ff4186 | 25 | * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs) |
ansond | 0:137634ff4186 | 26 | * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs) |
ansond | 0:137634ff4186 | 27 | * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10) |
ansond | 0:137634ff4186 | 28 | * |
ansond | 0:137634ff4186 | 29 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf |
ansond | 0:137634ff4186 | 30 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf |
ansond | 0:137634ff4186 | 31 | */ |
ansond | 0:137634ff4186 | 32 | |
ansond | 0:137634ff4186 | 33 | #if !defined(POLARSSL_CONFIG_FILE) |
ansond | 0:137634ff4186 | 34 | #include "polarssl/config.h" |
ansond | 0:137634ff4186 | 35 | #else |
ansond | 0:137634ff4186 | 36 | #include POLARSSL_CONFIG_FILE |
ansond | 0:137634ff4186 | 37 | #endif |
ansond | 0:137634ff4186 | 38 | |
ansond | 0:137634ff4186 | 39 | #if defined(POLARSSL_X509_CSR_PARSE_C) |
ansond | 0:137634ff4186 | 40 | |
ansond | 0:137634ff4186 | 41 | #include "polarssl/x509_csr.h" |
ansond | 0:137634ff4186 | 42 | #include "polarssl/oid.h" |
ansond | 0:137634ff4186 | 43 | |
ansond | 0:137634ff4186 | 44 | #include <string.h> |
ansond | 0:137634ff4186 | 45 | |
ansond | 0:137634ff4186 | 46 | #if defined(POLARSSL_PEM_PARSE_C) |
ansond | 0:137634ff4186 | 47 | #include "polarssl/pem.h" |
ansond | 0:137634ff4186 | 48 | #endif |
ansond | 0:137634ff4186 | 49 | |
ansond | 0:137634ff4186 | 50 | #if defined(POLARSSL_PLATFORM_C) |
ansond | 0:137634ff4186 | 51 | #include "polarssl/platform.h" |
ansond | 0:137634ff4186 | 52 | #else |
ansond | 0:137634ff4186 | 53 | #include <stdlib.h> |
ansond | 0:137634ff4186 | 54 | #include <stdio.h> |
ansond | 0:137634ff4186 | 55 | #define polarssl_free free |
ansond | 0:137634ff4186 | 56 | #define polarssl_malloc malloc |
ansond | 0:137634ff4186 | 57 | #define polarssl_snprintf snprintf |
ansond | 0:137634ff4186 | 58 | #endif |
ansond | 0:137634ff4186 | 59 | |
ansond | 0:137634ff4186 | 60 | #if defined(POLARSSL_FS_IO) || defined(EFIX64) || defined(EFI32) |
ansond | 0:137634ff4186 | 61 | #include <stdio.h> |
ansond | 0:137634ff4186 | 62 | #endif |
ansond | 0:137634ff4186 | 63 | |
ansond | 0:137634ff4186 | 64 | /* Implementation that should never be optimized out by the compiler */ |
ansond | 0:137634ff4186 | 65 | static void polarssl_zeroize( void *v, size_t n ) { |
ansond | 0:137634ff4186 | 66 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
ansond | 0:137634ff4186 | 67 | } |
ansond | 0:137634ff4186 | 68 | |
ansond | 0:137634ff4186 | 69 | /* |
ansond | 0:137634ff4186 | 70 | * Version ::= INTEGER { v1(0) } |
ansond | 0:137634ff4186 | 71 | */ |
ansond | 0:137634ff4186 | 72 | static int x509_csr_get_version( unsigned char **p, |
ansond | 0:137634ff4186 | 73 | const unsigned char *end, |
ansond | 0:137634ff4186 | 74 | int *ver ) |
ansond | 0:137634ff4186 | 75 | { |
ansond | 0:137634ff4186 | 76 | int ret; |
ansond | 0:137634ff4186 | 77 | |
ansond | 0:137634ff4186 | 78 | if( ( ret = asn1_get_int( p, end, ver ) ) != 0 ) |
ansond | 0:137634ff4186 | 79 | { |
ansond | 0:137634ff4186 | 80 | if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) |
ansond | 0:137634ff4186 | 81 | { |
ansond | 0:137634ff4186 | 82 | *ver = 0; |
ansond | 0:137634ff4186 | 83 | return( 0 ); |
ansond | 0:137634ff4186 | 84 | } |
ansond | 0:137634ff4186 | 85 | |
ansond | 0:137634ff4186 | 86 | return( POLARSSL_ERR_X509_INVALID_VERSION + ret ); |
ansond | 0:137634ff4186 | 87 | } |
ansond | 0:137634ff4186 | 88 | |
ansond | 0:137634ff4186 | 89 | return( 0 ); |
ansond | 0:137634ff4186 | 90 | } |
ansond | 0:137634ff4186 | 91 | |
ansond | 0:137634ff4186 | 92 | /* |
ansond | 0:137634ff4186 | 93 | * Parse a CSR in DER format |
ansond | 0:137634ff4186 | 94 | */ |
ansond | 0:137634ff4186 | 95 | int x509_csr_parse_der( x509_csr *csr, |
ansond | 0:137634ff4186 | 96 | const unsigned char *buf, size_t buflen ) |
ansond | 0:137634ff4186 | 97 | { |
ansond | 0:137634ff4186 | 98 | int ret; |
ansond | 0:137634ff4186 | 99 | size_t len; |
ansond | 0:137634ff4186 | 100 | unsigned char *p, *end; |
ansond | 0:137634ff4186 | 101 | x509_buf sig_params; |
ansond | 0:137634ff4186 | 102 | |
ansond | 0:137634ff4186 | 103 | memset( &sig_params, 0, sizeof( x509_buf ) ); |
ansond | 0:137634ff4186 | 104 | |
ansond | 0:137634ff4186 | 105 | /* |
ansond | 0:137634ff4186 | 106 | * Check for valid input |
ansond | 0:137634ff4186 | 107 | */ |
ansond | 0:137634ff4186 | 108 | if( csr == NULL || buf == NULL ) |
ansond | 0:137634ff4186 | 109 | return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); |
ansond | 0:137634ff4186 | 110 | |
ansond | 0:137634ff4186 | 111 | x509_csr_init( csr ); |
ansond | 0:137634ff4186 | 112 | |
ansond | 0:137634ff4186 | 113 | /* |
ansond | 0:137634ff4186 | 114 | * first copy the raw DER data |
ansond | 0:137634ff4186 | 115 | */ |
ansond | 0:137634ff4186 | 116 | p = polarssl_malloc( len = buflen ); |
ansond | 0:137634ff4186 | 117 | |
ansond | 0:137634ff4186 | 118 | if( p == NULL ) |
ansond | 0:137634ff4186 | 119 | return( POLARSSL_ERR_X509_MALLOC_FAILED ); |
ansond | 0:137634ff4186 | 120 | |
ansond | 0:137634ff4186 | 121 | memcpy( p, buf, buflen ); |
ansond | 0:137634ff4186 | 122 | |
ansond | 0:137634ff4186 | 123 | csr->raw.p = p; |
ansond | 0:137634ff4186 | 124 | csr->raw.len = len; |
ansond | 0:137634ff4186 | 125 | end = p + len; |
ansond | 0:137634ff4186 | 126 | |
ansond | 0:137634ff4186 | 127 | /* |
ansond | 0:137634ff4186 | 128 | * CertificationRequest ::= SEQUENCE { |
ansond | 0:137634ff4186 | 129 | * certificationRequestInfo CertificationRequestInfo, |
ansond | 0:137634ff4186 | 130 | * signatureAlgorithm AlgorithmIdentifier, |
ansond | 0:137634ff4186 | 131 | * signature BIT STRING |
ansond | 0:137634ff4186 | 132 | * } |
ansond | 0:137634ff4186 | 133 | */ |
ansond | 0:137634ff4186 | 134 | if( ( ret = asn1_get_tag( &p, end, &len, |
ansond | 0:137634ff4186 | 135 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
ansond | 0:137634ff4186 | 136 | { |
ansond | 0:137634ff4186 | 137 | x509_csr_free( csr ); |
ansond | 0:137634ff4186 | 138 | return( POLARSSL_ERR_X509_INVALID_FORMAT ); |
ansond | 0:137634ff4186 | 139 | } |
ansond | 0:137634ff4186 | 140 | |
ansond | 0:137634ff4186 | 141 | if( len != (size_t) ( end - p ) ) |
ansond | 0:137634ff4186 | 142 | { |
ansond | 0:137634ff4186 | 143 | x509_csr_free( csr ); |
ansond | 0:137634ff4186 | 144 | return( POLARSSL_ERR_X509_INVALID_FORMAT + |
ansond | 0:137634ff4186 | 145 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
ansond | 0:137634ff4186 | 146 | } |
ansond | 0:137634ff4186 | 147 | |
ansond | 0:137634ff4186 | 148 | /* |
ansond | 0:137634ff4186 | 149 | * CertificationRequestInfo ::= SEQUENCE { |
ansond | 0:137634ff4186 | 150 | */ |
ansond | 0:137634ff4186 | 151 | csr->cri.p = p; |
ansond | 0:137634ff4186 | 152 | |
ansond | 0:137634ff4186 | 153 | if( ( ret = asn1_get_tag( &p, end, &len, |
ansond | 0:137634ff4186 | 154 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
ansond | 0:137634ff4186 | 155 | { |
ansond | 0:137634ff4186 | 156 | x509_csr_free( csr ); |
ansond | 0:137634ff4186 | 157 | return( POLARSSL_ERR_X509_INVALID_FORMAT + ret ); |
ansond | 0:137634ff4186 | 158 | } |
ansond | 0:137634ff4186 | 159 | |
ansond | 0:137634ff4186 | 160 | end = p + len; |
ansond | 0:137634ff4186 | 161 | csr->cri.len = end - csr->cri.p; |
ansond | 0:137634ff4186 | 162 | |
ansond | 0:137634ff4186 | 163 | /* |
ansond | 0:137634ff4186 | 164 | * Version ::= INTEGER { v1(0) } |
ansond | 0:137634ff4186 | 165 | */ |
ansond | 0:137634ff4186 | 166 | if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 ) |
ansond | 0:137634ff4186 | 167 | { |
ansond | 0:137634ff4186 | 168 | x509_csr_free( csr ); |
ansond | 0:137634ff4186 | 169 | return( ret ); |
ansond | 0:137634ff4186 | 170 | } |
ansond | 0:137634ff4186 | 171 | |
ansond | 0:137634ff4186 | 172 | csr->version++; |
ansond | 0:137634ff4186 | 173 | |
ansond | 0:137634ff4186 | 174 | if( csr->version != 1 ) |
ansond | 0:137634ff4186 | 175 | { |
ansond | 0:137634ff4186 | 176 | x509_csr_free( csr ); |
ansond | 0:137634ff4186 | 177 | return( POLARSSL_ERR_X509_UNKNOWN_VERSION ); |
ansond | 0:137634ff4186 | 178 | } |
ansond | 0:137634ff4186 | 179 | |
ansond | 0:137634ff4186 | 180 | /* |
ansond | 0:137634ff4186 | 181 | * subject Name |
ansond | 0:137634ff4186 | 182 | */ |
ansond | 0:137634ff4186 | 183 | csr->subject_raw.p = p; |
ansond | 0:137634ff4186 | 184 | |
ansond | 0:137634ff4186 | 185 | if( ( ret = asn1_get_tag( &p, end, &len, |
ansond | 0:137634ff4186 | 186 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
ansond | 0:137634ff4186 | 187 | { |
ansond | 0:137634ff4186 | 188 | x509_csr_free( csr ); |
ansond | 0:137634ff4186 | 189 | return( POLARSSL_ERR_X509_INVALID_FORMAT + ret ); |
ansond | 0:137634ff4186 | 190 | } |
ansond | 0:137634ff4186 | 191 | |
ansond | 0:137634ff4186 | 192 | if( ( ret = x509_get_name( &p, p + len, &csr->subject ) ) != 0 ) |
ansond | 0:137634ff4186 | 193 | { |
ansond | 0:137634ff4186 | 194 | x509_csr_free( csr ); |
ansond | 0:137634ff4186 | 195 | return( ret ); |
ansond | 0:137634ff4186 | 196 | } |
ansond | 0:137634ff4186 | 197 | |
ansond | 0:137634ff4186 | 198 | csr->subject_raw.len = p - csr->subject_raw.p; |
ansond | 0:137634ff4186 | 199 | |
ansond | 0:137634ff4186 | 200 | /* |
ansond | 0:137634ff4186 | 201 | * subjectPKInfo SubjectPublicKeyInfo |
ansond | 0:137634ff4186 | 202 | */ |
ansond | 0:137634ff4186 | 203 | if( ( ret = pk_parse_subpubkey( &p, end, &csr->pk ) ) != 0 ) |
ansond | 0:137634ff4186 | 204 | { |
ansond | 0:137634ff4186 | 205 | x509_csr_free( csr ); |
ansond | 0:137634ff4186 | 206 | return( ret ); |
ansond | 0:137634ff4186 | 207 | } |
ansond | 0:137634ff4186 | 208 | |
ansond | 0:137634ff4186 | 209 | /* |
ansond | 0:137634ff4186 | 210 | * attributes [0] Attributes |
ansond | 0:137634ff4186 | 211 | */ |
ansond | 0:137634ff4186 | 212 | if( ( ret = asn1_get_tag( &p, end, &len, |
ansond | 0:137634ff4186 | 213 | ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC ) ) != 0 ) |
ansond | 0:137634ff4186 | 214 | { |
ansond | 0:137634ff4186 | 215 | x509_csr_free( csr ); |
ansond | 0:137634ff4186 | 216 | return( POLARSSL_ERR_X509_INVALID_FORMAT + ret ); |
ansond | 0:137634ff4186 | 217 | } |
ansond | 0:137634ff4186 | 218 | // TODO Parse Attributes / extension requests |
ansond | 0:137634ff4186 | 219 | |
ansond | 0:137634ff4186 | 220 | p += len; |
ansond | 0:137634ff4186 | 221 | |
ansond | 0:137634ff4186 | 222 | end = csr->raw.p + csr->raw.len; |
ansond | 0:137634ff4186 | 223 | |
ansond | 0:137634ff4186 | 224 | /* |
ansond | 0:137634ff4186 | 225 | * signatureAlgorithm AlgorithmIdentifier, |
ansond | 0:137634ff4186 | 226 | * signature BIT STRING |
ansond | 0:137634ff4186 | 227 | */ |
ansond | 0:137634ff4186 | 228 | if( ( ret = x509_get_alg( &p, end, &csr->sig_oid, &sig_params ) ) != 0 ) |
ansond | 0:137634ff4186 | 229 | { |
ansond | 0:137634ff4186 | 230 | x509_csr_free( csr ); |
ansond | 0:137634ff4186 | 231 | return( ret ); |
ansond | 0:137634ff4186 | 232 | } |
ansond | 0:137634ff4186 | 233 | |
ansond | 0:137634ff4186 | 234 | if( ( ret = x509_get_sig_alg( &csr->sig_oid, &sig_params, |
ansond | 0:137634ff4186 | 235 | &csr->sig_md, &csr->sig_pk, |
ansond | 0:137634ff4186 | 236 | &csr->sig_opts ) ) != 0 ) |
ansond | 0:137634ff4186 | 237 | { |
ansond | 0:137634ff4186 | 238 | x509_csr_free( csr ); |
ansond | 0:137634ff4186 | 239 | return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG ); |
ansond | 0:137634ff4186 | 240 | } |
ansond | 0:137634ff4186 | 241 | |
ansond | 0:137634ff4186 | 242 | if( ( ret = x509_get_sig( &p, end, &csr->sig ) ) != 0 ) |
ansond | 0:137634ff4186 | 243 | { |
ansond | 0:137634ff4186 | 244 | x509_csr_free( csr ); |
ansond | 0:137634ff4186 | 245 | return( ret ); |
ansond | 0:137634ff4186 | 246 | } |
ansond | 0:137634ff4186 | 247 | |
ansond | 0:137634ff4186 | 248 | if( p != end ) |
ansond | 0:137634ff4186 | 249 | { |
ansond | 0:137634ff4186 | 250 | x509_csr_free( csr ); |
ansond | 0:137634ff4186 | 251 | return( POLARSSL_ERR_X509_INVALID_FORMAT + |
ansond | 0:137634ff4186 | 252 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
ansond | 0:137634ff4186 | 253 | } |
ansond | 0:137634ff4186 | 254 | |
ansond | 0:137634ff4186 | 255 | return( 0 ); |
ansond | 0:137634ff4186 | 256 | } |
ansond | 0:137634ff4186 | 257 | |
ansond | 0:137634ff4186 | 258 | /* |
ansond | 0:137634ff4186 | 259 | * Parse a CSR, allowing for PEM or raw DER encoding |
ansond | 0:137634ff4186 | 260 | */ |
ansond | 0:137634ff4186 | 261 | int x509_csr_parse( x509_csr *csr, const unsigned char *buf, size_t buflen ) |
ansond | 0:137634ff4186 | 262 | { |
ansond | 0:137634ff4186 | 263 | int ret; |
ansond | 0:137634ff4186 | 264 | #if defined(POLARSSL_PEM_PARSE_C) |
ansond | 0:137634ff4186 | 265 | size_t use_len; |
ansond | 0:137634ff4186 | 266 | pem_context pem; |
ansond | 0:137634ff4186 | 267 | #endif |
ansond | 0:137634ff4186 | 268 | |
ansond | 0:137634ff4186 | 269 | /* |
ansond | 0:137634ff4186 | 270 | * Check for valid input |
ansond | 0:137634ff4186 | 271 | */ |
ansond | 0:137634ff4186 | 272 | if( csr == NULL || buf == NULL ) |
ansond | 0:137634ff4186 | 273 | return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); |
ansond | 0:137634ff4186 | 274 | |
ansond | 0:137634ff4186 | 275 | #if defined(POLARSSL_PEM_PARSE_C) |
ansond | 0:137634ff4186 | 276 | pem_init( &pem ); |
ansond | 0:137634ff4186 | 277 | ret = pem_read_buffer( &pem, |
ansond | 0:137634ff4186 | 278 | "-----BEGIN CERTIFICATE REQUEST-----", |
ansond | 0:137634ff4186 | 279 | "-----END CERTIFICATE REQUEST-----", |
ansond | 0:137634ff4186 | 280 | buf, NULL, 0, &use_len ); |
ansond | 0:137634ff4186 | 281 | |
ansond | 0:137634ff4186 | 282 | if( ret == 0 ) |
ansond | 0:137634ff4186 | 283 | { |
ansond | 0:137634ff4186 | 284 | /* |
ansond | 0:137634ff4186 | 285 | * Was PEM encoded, parse the result |
ansond | 0:137634ff4186 | 286 | */ |
ansond | 0:137634ff4186 | 287 | if( ( ret = x509_csr_parse_der( csr, pem.buf, pem.buflen ) ) != 0 ) |
ansond | 0:137634ff4186 | 288 | return( ret ); |
ansond | 0:137634ff4186 | 289 | |
ansond | 0:137634ff4186 | 290 | pem_free( &pem ); |
ansond | 0:137634ff4186 | 291 | return( 0 ); |
ansond | 0:137634ff4186 | 292 | } |
ansond | 0:137634ff4186 | 293 | else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) |
ansond | 0:137634ff4186 | 294 | { |
ansond | 0:137634ff4186 | 295 | pem_free( &pem ); |
ansond | 0:137634ff4186 | 296 | return( ret ); |
ansond | 0:137634ff4186 | 297 | } |
ansond | 0:137634ff4186 | 298 | else |
ansond | 0:137634ff4186 | 299 | #endif /* POLARSSL_PEM_PARSE_C */ |
ansond | 0:137634ff4186 | 300 | return( x509_csr_parse_der( csr, buf, buflen ) ); |
ansond | 0:137634ff4186 | 301 | } |
ansond | 0:137634ff4186 | 302 | |
ansond | 0:137634ff4186 | 303 | #if defined(POLARSSL_FS_IO) |
ansond | 0:137634ff4186 | 304 | /* |
ansond | 0:137634ff4186 | 305 | * Load a CSR into the structure |
ansond | 0:137634ff4186 | 306 | */ |
ansond | 0:137634ff4186 | 307 | int x509_csr_parse_file( x509_csr *csr, const char *path ) |
ansond | 0:137634ff4186 | 308 | { |
ansond | 0:137634ff4186 | 309 | int ret; |
ansond | 0:137634ff4186 | 310 | size_t n; |
ansond | 0:137634ff4186 | 311 | unsigned char *buf; |
ansond | 0:137634ff4186 | 312 | |
ansond | 0:137634ff4186 | 313 | if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 ) |
ansond | 0:137634ff4186 | 314 | return( ret ); |
ansond | 0:137634ff4186 | 315 | |
ansond | 0:137634ff4186 | 316 | ret = x509_csr_parse( csr, buf, n ); |
ansond | 0:137634ff4186 | 317 | |
ansond | 0:137634ff4186 | 318 | polarssl_zeroize( buf, n + 1 ); |
ansond | 0:137634ff4186 | 319 | polarssl_free( buf ); |
ansond | 0:137634ff4186 | 320 | |
ansond | 0:137634ff4186 | 321 | return( ret ); |
ansond | 0:137634ff4186 | 322 | } |
ansond | 0:137634ff4186 | 323 | #endif /* POLARSSL_FS_IO */ |
ansond | 0:137634ff4186 | 324 | |
ansond | 0:137634ff4186 | 325 | #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \ |
ansond | 0:137634ff4186 | 326 | !defined(EFI32) |
ansond | 0:137634ff4186 | 327 | #include <stdarg.h> |
ansond | 0:137634ff4186 | 328 | |
ansond | 0:137634ff4186 | 329 | #if !defined vsnprintf |
ansond | 0:137634ff4186 | 330 | #define vsnprintf _vsnprintf |
ansond | 0:137634ff4186 | 331 | #endif // vsnprintf |
ansond | 0:137634ff4186 | 332 | |
ansond | 0:137634ff4186 | 333 | /* |
ansond | 0:137634ff4186 | 334 | * Windows _snprintf and _vsnprintf are not compatible to linux versions. |
ansond | 0:137634ff4186 | 335 | * Result value is not size of buffer needed, but -1 if no fit is possible. |
ansond | 0:137634ff4186 | 336 | * |
ansond | 0:137634ff4186 | 337 | * This fuction tries to 'fix' this by at least suggesting enlarging the |
ansond | 0:137634ff4186 | 338 | * size by 20. |
ansond | 0:137634ff4186 | 339 | */ |
ansond | 0:137634ff4186 | 340 | static int compat_snprintf( char *str, size_t size, const char *format, ... ) |
ansond | 0:137634ff4186 | 341 | { |
ansond | 0:137634ff4186 | 342 | va_list ap; |
ansond | 0:137634ff4186 | 343 | int res = -1; |
ansond | 0:137634ff4186 | 344 | |
ansond | 0:137634ff4186 | 345 | va_start( ap, format ); |
ansond | 0:137634ff4186 | 346 | |
ansond | 0:137634ff4186 | 347 | res = vsnprintf( str, size, format, ap ); |
ansond | 0:137634ff4186 | 348 | |
ansond | 0:137634ff4186 | 349 | va_end( ap ); |
ansond | 0:137634ff4186 | 350 | |
ansond | 0:137634ff4186 | 351 | // No quick fix possible |
ansond | 0:137634ff4186 | 352 | if( res < 0 ) |
ansond | 0:137634ff4186 | 353 | return( (int) size + 20 ); |
ansond | 0:137634ff4186 | 354 | |
ansond | 0:137634ff4186 | 355 | return( res ); |
ansond | 0:137634ff4186 | 356 | } |
ansond | 0:137634ff4186 | 357 | |
ansond | 0:137634ff4186 | 358 | #define snprintf compat_snprintf |
ansond | 0:137634ff4186 | 359 | #endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */ |
ansond | 0:137634ff4186 | 360 | |
ansond | 0:137634ff4186 | 361 | #define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2 |
ansond | 0:137634ff4186 | 362 | |
ansond | 0:137634ff4186 | 363 | #define SAFE_SNPRINTF() \ |
ansond | 0:137634ff4186 | 364 | { \ |
ansond | 0:137634ff4186 | 365 | if( ret == -1 ) \ |
ansond | 0:137634ff4186 | 366 | return( -1 ); \ |
ansond | 0:137634ff4186 | 367 | \ |
ansond | 0:137634ff4186 | 368 | if( (unsigned int) ret > n ) { \ |
ansond | 0:137634ff4186 | 369 | p[n - 1] = '\0'; \ |
ansond | 0:137634ff4186 | 370 | return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \ |
ansond | 0:137634ff4186 | 371 | } \ |
ansond | 0:137634ff4186 | 372 | \ |
ansond | 0:137634ff4186 | 373 | n -= (unsigned int) ret; \ |
ansond | 0:137634ff4186 | 374 | p += (unsigned int) ret; \ |
ansond | 0:137634ff4186 | 375 | } |
ansond | 0:137634ff4186 | 376 | |
ansond | 0:137634ff4186 | 377 | #define BEFORE_COLON 14 |
ansond | 0:137634ff4186 | 378 | #define BC "14" |
ansond | 0:137634ff4186 | 379 | /* |
ansond | 0:137634ff4186 | 380 | * Return an informational string about the CSR. |
ansond | 0:137634ff4186 | 381 | */ |
ansond | 0:137634ff4186 | 382 | int x509_csr_info( char *buf, size_t size, const char *prefix, |
ansond | 0:137634ff4186 | 383 | const x509_csr *csr ) |
ansond | 0:137634ff4186 | 384 | { |
ansond | 0:137634ff4186 | 385 | int ret; |
ansond | 0:137634ff4186 | 386 | size_t n; |
ansond | 0:137634ff4186 | 387 | char *p; |
ansond | 0:137634ff4186 | 388 | char key_size_str[BEFORE_COLON]; |
ansond | 0:137634ff4186 | 389 | |
ansond | 0:137634ff4186 | 390 | p = buf; |
ansond | 0:137634ff4186 | 391 | n = size; |
ansond | 0:137634ff4186 | 392 | |
ansond | 0:137634ff4186 | 393 | ret = polarssl_snprintf( p, n, "%sCSR version : %d", |
ansond | 0:137634ff4186 | 394 | prefix, csr->version ); |
ansond | 0:137634ff4186 | 395 | SAFE_SNPRINTF(); |
ansond | 0:137634ff4186 | 396 | |
ansond | 0:137634ff4186 | 397 | ret = polarssl_snprintf( p, n, "\n%ssubject name : ", prefix ); |
ansond | 0:137634ff4186 | 398 | SAFE_SNPRINTF(); |
ansond | 0:137634ff4186 | 399 | ret = x509_dn_gets( p, n, &csr->subject ); |
ansond | 0:137634ff4186 | 400 | SAFE_SNPRINTF(); |
ansond | 0:137634ff4186 | 401 | |
ansond | 0:137634ff4186 | 402 | ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix ); |
ansond | 0:137634ff4186 | 403 | SAFE_SNPRINTF(); |
ansond | 0:137634ff4186 | 404 | |
ansond | 0:137634ff4186 | 405 | ret = x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md, |
ansond | 0:137634ff4186 | 406 | csr->sig_opts ); |
ansond | 0:137634ff4186 | 407 | SAFE_SNPRINTF(); |
ansond | 0:137634ff4186 | 408 | |
ansond | 0:137634ff4186 | 409 | if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON, |
ansond | 0:137634ff4186 | 410 | pk_get_name( &csr->pk ) ) ) != 0 ) |
ansond | 0:137634ff4186 | 411 | { |
ansond | 0:137634ff4186 | 412 | return( ret ); |
ansond | 0:137634ff4186 | 413 | } |
ansond | 0:137634ff4186 | 414 | |
ansond | 0:137634ff4186 | 415 | ret = polarssl_snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str, |
ansond | 0:137634ff4186 | 416 | (int) pk_get_size( &csr->pk ) ); |
ansond | 0:137634ff4186 | 417 | SAFE_SNPRINTF(); |
ansond | 0:137634ff4186 | 418 | |
ansond | 0:137634ff4186 | 419 | return( (int) ( size - n ) ); |
ansond | 0:137634ff4186 | 420 | } |
ansond | 0:137634ff4186 | 421 | |
ansond | 0:137634ff4186 | 422 | /* |
ansond | 0:137634ff4186 | 423 | * Initialize a CSR |
ansond | 0:137634ff4186 | 424 | */ |
ansond | 0:137634ff4186 | 425 | void x509_csr_init( x509_csr *csr ) |
ansond | 0:137634ff4186 | 426 | { |
ansond | 0:137634ff4186 | 427 | memset( csr, 0, sizeof(x509_csr) ); |
ansond | 0:137634ff4186 | 428 | } |
ansond | 0:137634ff4186 | 429 | |
ansond | 0:137634ff4186 | 430 | /* |
ansond | 0:137634ff4186 | 431 | * Unallocate all CSR data |
ansond | 0:137634ff4186 | 432 | */ |
ansond | 0:137634ff4186 | 433 | void x509_csr_free( x509_csr *csr ) |
ansond | 0:137634ff4186 | 434 | { |
ansond | 0:137634ff4186 | 435 | x509_name *name_cur; |
ansond | 0:137634ff4186 | 436 | x509_name *name_prv; |
ansond | 0:137634ff4186 | 437 | |
ansond | 0:137634ff4186 | 438 | if( csr == NULL ) |
ansond | 0:137634ff4186 | 439 | return; |
ansond | 0:137634ff4186 | 440 | |
ansond | 0:137634ff4186 | 441 | pk_free( &csr->pk ); |
ansond | 0:137634ff4186 | 442 | |
ansond | 0:137634ff4186 | 443 | #if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT) |
ansond | 0:137634ff4186 | 444 | polarssl_free( csr->sig_opts ); |
ansond | 0:137634ff4186 | 445 | #endif |
ansond | 0:137634ff4186 | 446 | |
ansond | 0:137634ff4186 | 447 | name_cur = csr->subject.next; |
ansond | 0:137634ff4186 | 448 | while( name_cur != NULL ) |
ansond | 0:137634ff4186 | 449 | { |
ansond | 0:137634ff4186 | 450 | name_prv = name_cur; |
ansond | 0:137634ff4186 | 451 | name_cur = name_cur->next; |
ansond | 0:137634ff4186 | 452 | polarssl_zeroize( name_prv, sizeof( x509_name ) ); |
ansond | 0:137634ff4186 | 453 | polarssl_free( name_prv ); |
ansond | 0:137634ff4186 | 454 | } |
ansond | 0:137634ff4186 | 455 | |
ansond | 0:137634ff4186 | 456 | if( csr->raw.p != NULL ) |
ansond | 0:137634ff4186 | 457 | { |
ansond | 0:137634ff4186 | 458 | polarssl_zeroize( csr->raw.p, csr->raw.len ); |
ansond | 0:137634ff4186 | 459 | polarssl_free( csr->raw.p ); |
ansond | 0:137634ff4186 | 460 | } |
ansond | 0:137634ff4186 | 461 | |
ansond | 0:137634ff4186 | 462 | polarssl_zeroize( csr, sizeof( x509_csr ) ); |
ansond | 0:137634ff4186 | 463 | } |
ansond | 0:137634ff4186 | 464 | |
ansond | 0:137634ff4186 | 465 | #endif /* POLARSSL_X509_CSR_PARSE_C */ |
ansond | 0:137634ff4186 | 466 |