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