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