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