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