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