Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * X.509 certificate writing |
| 3 | * |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [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 | * References: |
| 27 | * - certificates: RFC 5280, updated by RFC 6818 |
| 28 | * - CSRs: PKCS#10 v1.7 aka RFC 2986 |
| 29 | * - attributes: PKCS#9 v2.0 aka RFC 2985 |
| 30 | */ |
| 31 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 33 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #else |
| 35 | #include POLARSSL_CONFIG_FILE |
| 36 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 37 | |
| 38 | #if defined(POLARSSL_X509_CRT_WRITE_C) |
| 39 | |
| 40 | #include "polarssl/x509_crt.h" |
| 41 | #include "polarssl/oid.h" |
| 42 | #include "polarssl/asn1write.h" |
| 43 | #include "polarssl/sha1.h" |
| 44 | |
| 45 | #if defined(POLARSSL_PEM_WRITE_C) |
| 46 | #include "polarssl/pem.h" |
| 47 | #endif /* POLARSSL_PEM_WRITE_C */ |
| 48 | |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 49 | /* Implementation that should never be optimized out by the compiler */ |
| 50 | static void polarssl_zeroize( void *v, size_t n ) { |
| 51 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 52 | } |
| 53 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 54 | void x509write_crt_init( x509write_cert *ctx ) |
| 55 | { |
| 56 | memset( ctx, 0, sizeof(x509write_cert) ); |
| 57 | |
| 58 | mpi_init( &ctx->serial ); |
| 59 | ctx->version = X509_CRT_VERSION_3; |
| 60 | } |
| 61 | |
| 62 | void x509write_crt_free( x509write_cert *ctx ) |
| 63 | { |
| 64 | mpi_free( &ctx->serial ); |
| 65 | |
| 66 | asn1_free_named_data_list( &ctx->subject ); |
| 67 | asn1_free_named_data_list( &ctx->issuer ); |
| 68 | asn1_free_named_data_list( &ctx->extensions ); |
| 69 | |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 70 | polarssl_zeroize( ctx, sizeof(x509write_cert) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Paul Bakker | 5191e92 | 2013-10-11 10:54:28 +0200 | [diff] [blame] | 73 | void x509write_crt_set_version( x509write_cert *ctx, int version ) |
| 74 | { |
| 75 | ctx->version = version; |
| 76 | } |
| 77 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 78 | void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg ) |
| 79 | { |
| 80 | ctx->md_alg = md_alg; |
| 81 | } |
| 82 | |
| 83 | void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key ) |
| 84 | { |
| 85 | ctx->subject_key = key; |
| 86 | } |
| 87 | |
| 88 | void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key ) |
| 89 | { |
| 90 | ctx->issuer_key = key; |
| 91 | } |
| 92 | |
Paul Bakker | 50dc850 | 2013-10-28 21:19:10 +0100 | [diff] [blame] | 93 | int x509write_crt_set_subject_name( x509write_cert *ctx, |
| 94 | const char *subject_name ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 95 | { |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 96 | return x509_string_to_names( &ctx->subject, subject_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Paul Bakker | 50dc850 | 2013-10-28 21:19:10 +0100 | [diff] [blame] | 99 | int x509write_crt_set_issuer_name( x509write_cert *ctx, |
| 100 | const char *issuer_name ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 101 | { |
Paul Bakker | 86d0c19 | 2013-09-18 11:11:02 +0200 | [diff] [blame] | 102 | return x509_string_to_names( &ctx->issuer, issuer_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial ) |
| 106 | { |
| 107 | int ret; |
| 108 | |
| 109 | if( ( ret = mpi_copy( &ctx->serial, serial ) ) != 0 ) |
| 110 | return( ret ); |
| 111 | |
| 112 | return( 0 ); |
| 113 | } |
| 114 | |
Paul Bakker | 50dc850 | 2013-10-28 21:19:10 +0100 | [diff] [blame] | 115 | int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before, |
| 116 | const char *not_after ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 117 | { |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 118 | if( strlen( not_before ) != X509_RFC5280_UTC_TIME_LEN - 1 || |
| 119 | strlen( not_after ) != X509_RFC5280_UTC_TIME_LEN - 1 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 120 | { |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 121 | return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 122 | } |
| 123 | strncpy( ctx->not_before, not_before, X509_RFC5280_UTC_TIME_LEN ); |
| 124 | strncpy( ctx->not_after , not_after , X509_RFC5280_UTC_TIME_LEN ); |
| 125 | ctx->not_before[X509_RFC5280_UTC_TIME_LEN - 1] = 'Z'; |
| 126 | ctx->not_after[X509_RFC5280_UTC_TIME_LEN - 1] = 'Z'; |
| 127 | |
| 128 | return( 0 ); |
| 129 | } |
| 130 | |
| 131 | int x509write_crt_set_extension( x509write_cert *ctx, |
| 132 | const char *oid, size_t oid_len, |
| 133 | int critical, |
| 134 | const unsigned char *val, size_t val_len ) |
| 135 | { |
| 136 | return x509_set_extension( &ctx->extensions, oid, oid_len, |
| 137 | critical, val, val_len ); |
| 138 | } |
| 139 | |
| 140 | int x509write_crt_set_basic_constraints( x509write_cert *ctx, |
| 141 | int is_ca, int max_pathlen ) |
| 142 | { |
| 143 | int ret; |
| 144 | unsigned char buf[9]; |
| 145 | unsigned char *c = buf + sizeof(buf); |
| 146 | size_t len = 0; |
| 147 | |
| 148 | memset( buf, 0, sizeof(buf) ); |
| 149 | |
| 150 | if( is_ca && max_pathlen > 127 ) |
Paul Bakker | 5187656 | 2013-09-17 14:36:05 +0200 | [diff] [blame] | 151 | return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 152 | |
| 153 | if( is_ca ) |
| 154 | { |
| 155 | if( max_pathlen >= 0 ) |
| 156 | { |
| 157 | ASN1_CHK_ADD( len, asn1_write_int( &c, buf, max_pathlen ) ); |
| 158 | } |
| 159 | ASN1_CHK_ADD( len, asn1_write_bool( &c, buf, 1 ) ); |
| 160 | } |
| 161 | |
| 162 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 163 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | |
| 164 | ASN1_SEQUENCE ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 165 | |
| 166 | return x509write_crt_set_extension( ctx, OID_BASIC_CONSTRAINTS, |
| 167 | OID_SIZE( OID_BASIC_CONSTRAINTS ), |
| 168 | 0, buf + sizeof(buf) - len, len ); |
| 169 | } |
| 170 | |
Manuel Pégourié-Gonnard | 3daaf3d | 2013-10-27 14:22:02 +0100 | [diff] [blame] | 171 | #if defined(POLARSSL_SHA1_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 172 | int x509write_crt_set_subject_key_identifier( x509write_cert *ctx ) |
| 173 | { |
| 174 | int ret; |
| 175 | unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */ |
| 176 | unsigned char *c = buf + sizeof(buf); |
| 177 | size_t len = 0; |
| 178 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 179 | memset( buf, 0, sizeof(buf) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 180 | ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, ctx->subject_key ) ); |
| 181 | |
| 182 | sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 ); |
| 183 | c = buf + sizeof(buf) - 20; |
| 184 | len = 20; |
| 185 | |
| 186 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 187 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_OCTET_STRING ) ); |
| 188 | |
| 189 | return x509write_crt_set_extension( ctx, OID_SUBJECT_KEY_IDENTIFIER, |
| 190 | OID_SIZE( OID_SUBJECT_KEY_IDENTIFIER ), |
| 191 | 0, buf + sizeof(buf) - len, len ); |
| 192 | } |
| 193 | |
| 194 | int x509write_crt_set_authority_key_identifier( x509write_cert *ctx ) |
| 195 | { |
| 196 | int ret; |
| 197 | unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */ |
| 198 | unsigned char *c = buf + sizeof(buf); |
| 199 | size_t len = 0; |
| 200 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 201 | memset( buf, 0, sizeof(buf) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 202 | ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, ctx->issuer_key ) ); |
| 203 | |
| 204 | sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 ); |
| 205 | c = buf + sizeof(buf) - 20; |
| 206 | len = 20; |
| 207 | |
| 208 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 209 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONTEXT_SPECIFIC | 0 ) ); |
| 210 | |
| 211 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 212 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | |
| 213 | ASN1_SEQUENCE ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 214 | |
| 215 | return x509write_crt_set_extension( ctx, OID_AUTHORITY_KEY_IDENTIFIER, |
| 216 | OID_SIZE( OID_AUTHORITY_KEY_IDENTIFIER ), |
| 217 | 0, buf + sizeof(buf) - len, len ); |
| 218 | } |
Manuel Pégourié-Gonnard | 3daaf3d | 2013-10-27 14:22:02 +0100 | [diff] [blame] | 219 | #endif /* POLARSSL_SHA1_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 220 | |
| 221 | int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage ) |
| 222 | { |
| 223 | unsigned char buf[4]; |
| 224 | unsigned char *c; |
| 225 | int ret; |
| 226 | |
| 227 | c = buf + 4; |
| 228 | |
| 229 | if( ( ret = asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 ) |
| 230 | return( ret ); |
| 231 | |
| 232 | ret = x509write_crt_set_extension( ctx, OID_KEY_USAGE, |
| 233 | OID_SIZE( OID_KEY_USAGE ), |
| 234 | 1, buf, 4 ); |
| 235 | if( ret != 0 ) |
| 236 | return( ret ); |
| 237 | |
| 238 | return( 0 ); |
| 239 | } |
| 240 | |
| 241 | int x509write_crt_set_ns_cert_type( x509write_cert *ctx, |
| 242 | unsigned char ns_cert_type ) |
| 243 | { |
| 244 | unsigned char buf[4]; |
| 245 | unsigned char *c; |
| 246 | int ret; |
| 247 | |
| 248 | c = buf + 4; |
| 249 | |
| 250 | if( ( ret = asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 ) |
| 251 | return( ret ); |
| 252 | |
| 253 | ret = x509write_crt_set_extension( ctx, OID_NS_CERT_TYPE, |
| 254 | OID_SIZE( OID_NS_CERT_TYPE ), |
| 255 | 0, buf, 4 ); |
| 256 | if( ret != 0 ) |
| 257 | return( ret ); |
| 258 | |
| 259 | return( 0 ); |
| 260 | } |
| 261 | |
| 262 | static int x509_write_time( unsigned char **p, unsigned char *start, |
| 263 | const char *time, size_t size ) |
| 264 | { |
| 265 | int ret; |
| 266 | size_t len = 0; |
| 267 | |
| 268 | /* |
| 269 | * write ASN1_UTC_TIME if year < 2050 (2 bytes shorter) |
| 270 | */ |
| 271 | if( time[0] == '2' && time[1] == '0' && time [2] < '5' ) |
| 272 | { |
| 273 | ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, |
| 274 | (const unsigned char *) time + 2, |
| 275 | size - 2 ) ); |
| 276 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 277 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_UTC_TIME ) ); |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, |
| 282 | (const unsigned char *) time, |
| 283 | size ) ); |
| 284 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
| 285 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_GENERALIZED_TIME ) ); |
| 286 | } |
| 287 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 288 | return( (int) len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size, |
| 292 | int (*f_rng)(void *, unsigned char *, size_t), |
| 293 | void *p_rng ) |
| 294 | { |
| 295 | int ret; |
| 296 | const char *sig_oid; |
| 297 | size_t sig_oid_len = 0; |
| 298 | unsigned char *c, *c2; |
| 299 | unsigned char hash[64]; |
| 300 | unsigned char sig[POLARSSL_MPI_MAX_SIZE]; |
| 301 | unsigned char tmp_buf[2048]; |
| 302 | size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len; |
| 303 | size_t len = 0; |
| 304 | pk_type_t pk_alg; |
| 305 | |
| 306 | /* |
| 307 | * Prepare data to be signed in tmp_buf |
| 308 | */ |
| 309 | c = tmp_buf + sizeof( tmp_buf ); |
| 310 | |
| 311 | /* Signature algorithm needed in TBS, and later for actual signature */ |
| 312 | pk_alg = pk_get_type( ctx->issuer_key ); |
| 313 | if( pk_alg == POLARSSL_PK_ECKEY ) |
| 314 | pk_alg = POLARSSL_PK_ECDSA; |
| 315 | |
| 316 | if( ( ret = oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg, |
| 317 | &sig_oid, &sig_oid_len ) ) != 0 ) |
| 318 | { |
| 319 | return( ret ); |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
| 324 | */ |
| 325 | ASN1_CHK_ADD( len, x509_write_extensions( &c, tmp_buf, ctx->extensions ) ); |
| 326 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) ); |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 327 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | |
| 328 | ASN1_SEQUENCE ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 329 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) ); |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 330 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC | |
| 331 | ASN1_CONSTRUCTED | 3 ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 332 | |
| 333 | /* |
| 334 | * SubjectPublicKeyInfo |
| 335 | */ |
| 336 | ASN1_CHK_ADD( pub_len, pk_write_pubkey_der( ctx->subject_key, |
| 337 | tmp_buf, c - tmp_buf ) ); |
| 338 | c -= pub_len; |
| 339 | len += pub_len; |
| 340 | |
| 341 | /* |
| 342 | * Subject ::= Name |
| 343 | */ |
| 344 | ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->subject ) ); |
| 345 | |
| 346 | /* |
| 347 | * Validity ::= SEQUENCE { |
| 348 | * notBefore Time, |
| 349 | * notAfter Time } |
| 350 | */ |
| 351 | sub_len = 0; |
| 352 | |
| 353 | ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_after, |
| 354 | X509_RFC5280_UTC_TIME_LEN ) ); |
| 355 | |
| 356 | ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_before, |
| 357 | X509_RFC5280_UTC_TIME_LEN ) ); |
| 358 | |
| 359 | len += sub_len; |
| 360 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) ); |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 361 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | |
| 362 | ASN1_SEQUENCE ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 363 | |
| 364 | /* |
| 365 | * Issuer ::= Name |
| 366 | */ |
| 367 | ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->issuer ) ); |
| 368 | |
| 369 | /* |
| 370 | * Signature ::= AlgorithmIdentifier |
| 371 | */ |
| 372 | ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, tmp_buf, |
| 373 | sig_oid, strlen( sig_oid ), 0 ) ); |
| 374 | |
| 375 | /* |
| 376 | * Serial ::= INTEGER |
| 377 | */ |
| 378 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, tmp_buf, &ctx->serial ) ); |
| 379 | |
| 380 | /* |
| 381 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 382 | */ |
| 383 | sub_len = 0; |
| 384 | ASN1_CHK_ADD( sub_len, asn1_write_int( &c, tmp_buf, ctx->version ) ); |
| 385 | len += sub_len; |
| 386 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) ); |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 387 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC | |
| 388 | ASN1_CONSTRUCTED | 0 ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 389 | |
| 390 | ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) ); |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 391 | ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | |
| 392 | ASN1_SEQUENCE ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 393 | |
| 394 | /* |
| 395 | * Make signature |
| 396 | */ |
| 397 | md( md_info_from_type( ctx->md_alg ), c, len, hash ); |
| 398 | |
| 399 | if( ( ret = pk_sign( ctx->issuer_key, ctx->md_alg, hash, 0, sig, &sig_len, |
| 400 | f_rng, p_rng ) ) != 0 ) |
| 401 | { |
| 402 | return( ret ); |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Write data to output buffer |
| 407 | */ |
| 408 | c2 = buf + size; |
| 409 | ASN1_CHK_ADD( sig_and_oid_len, x509_write_sig( &c2, buf, |
| 410 | sig_oid, sig_oid_len, sig, sig_len ) ); |
| 411 | |
| 412 | c2 -= len; |
| 413 | memcpy( c2, c, len ); |
| 414 | |
| 415 | len += sig_and_oid_len; |
| 416 | ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) ); |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 417 | ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED | |
| 418 | ASN1_SEQUENCE ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 419 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 420 | return( (int) len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n" |
| 424 | #define PEM_END_CRT "-----END CERTIFICATE-----\n" |
| 425 | |
| 426 | #if defined(POLARSSL_PEM_WRITE_C) |
| 427 | int x509write_crt_pem( x509write_cert *crt, unsigned char *buf, size_t size, |
| 428 | int (*f_rng)(void *, unsigned char *, size_t), |
| 429 | void *p_rng ) |
| 430 | { |
| 431 | int ret; |
| 432 | unsigned char output_buf[4096]; |
| 433 | size_t olen = 0; |
| 434 | |
| 435 | if( ( ret = x509write_crt_der( crt, output_buf, sizeof(output_buf), |
| 436 | f_rng, p_rng ) ) < 0 ) |
| 437 | { |
| 438 | return( ret ); |
| 439 | } |
| 440 | |
| 441 | if( ( ret = pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT, |
| 442 | output_buf + sizeof(output_buf) - ret, |
| 443 | ret, buf, size, &olen ) ) != 0 ) |
| 444 | { |
| 445 | return( ret ); |
| 446 | } |
| 447 | |
| 448 | return( 0 ); |
| 449 | } |
| 450 | #endif /* POLARSSL_PEM_WRITE_C */ |
| 451 | |
| 452 | #endif /* POLARSSL_X509_CRT_WRITE_C */ |