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