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