Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 /*
switches 0:5c4d7b2438d3 2 * X.509 certificate writing
switches 0:5c4d7b2438d3 3 *
switches 0:5c4d7b2438d3 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
switches 0:5c4d7b2438d3 5 * SPDX-License-Identifier: Apache-2.0
switches 0:5c4d7b2438d3 6 *
switches 0:5c4d7b2438d3 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
switches 0:5c4d7b2438d3 8 * not use this file except in compliance with the License.
switches 0:5c4d7b2438d3 9 * You may obtain a copy of the License at
switches 0:5c4d7b2438d3 10 *
switches 0:5c4d7b2438d3 11 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:5c4d7b2438d3 12 *
switches 0:5c4d7b2438d3 13 * Unless required by applicable law or agreed to in writing, software
switches 0:5c4d7b2438d3 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
switches 0:5c4d7b2438d3 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:5c4d7b2438d3 16 * See the License for the specific language governing permissions and
switches 0:5c4d7b2438d3 17 * limitations under the License.
switches 0:5c4d7b2438d3 18 *
switches 0:5c4d7b2438d3 19 * This file is part of mbed TLS (https://tls.mbed.org)
switches 0:5c4d7b2438d3 20 */
switches 0:5c4d7b2438d3 21 /*
switches 0:5c4d7b2438d3 22 * References:
switches 0:5c4d7b2438d3 23 * - certificates: RFC 5280, updated by RFC 6818
switches 0:5c4d7b2438d3 24 * - CSRs: PKCS#10 v1.7 aka RFC 2986
switches 0:5c4d7b2438d3 25 * - attributes: PKCS#9 v2.0 aka RFC 2985
switches 0:5c4d7b2438d3 26 */
switches 0:5c4d7b2438d3 27
switches 0:5c4d7b2438d3 28 #if !defined(MBEDTLS_CONFIG_FILE)
switches 0:5c4d7b2438d3 29 #include "mbedtls/config.h"
switches 0:5c4d7b2438d3 30 #else
switches 0:5c4d7b2438d3 31 #include MBEDTLS_CONFIG_FILE
switches 0:5c4d7b2438d3 32 #endif
switches 0:5c4d7b2438d3 33
switches 0:5c4d7b2438d3 34 #if defined(MBEDTLS_X509_CRT_WRITE_C)
switches 0:5c4d7b2438d3 35
switches 0:5c4d7b2438d3 36 #include "mbedtls/x509_crt.h"
switches 0:5c4d7b2438d3 37 #include "mbedtls/oid.h"
switches 0:5c4d7b2438d3 38 #include "mbedtls/asn1write.h"
switches 0:5c4d7b2438d3 39 #include "mbedtls/sha1.h"
switches 0:5c4d7b2438d3 40
switches 0:5c4d7b2438d3 41 #include <string.h>
switches 0:5c4d7b2438d3 42
switches 0:5c4d7b2438d3 43 #if defined(MBEDTLS_PEM_WRITE_C)
switches 0:5c4d7b2438d3 44 #include "mbedtls/pem.h"
switches 0:5c4d7b2438d3 45 #endif /* MBEDTLS_PEM_WRITE_C */
switches 0:5c4d7b2438d3 46
switches 0:5c4d7b2438d3 47 /* Implementation that should never be optimized out by the compiler */
switches 0:5c4d7b2438d3 48 static void mbedtls_zeroize( void *v, size_t n ) {
switches 0:5c4d7b2438d3 49 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
switches 0:5c4d7b2438d3 50 }
switches 0:5c4d7b2438d3 51
switches 0:5c4d7b2438d3 52 void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx )
switches 0:5c4d7b2438d3 53 {
switches 0:5c4d7b2438d3 54 memset( ctx, 0, sizeof(mbedtls_x509write_cert) );
switches 0:5c4d7b2438d3 55
switches 0:5c4d7b2438d3 56 mbedtls_mpi_init( &ctx->serial );
switches 0:5c4d7b2438d3 57 ctx->version = MBEDTLS_X509_CRT_VERSION_3;
switches 0:5c4d7b2438d3 58 }
switches 0:5c4d7b2438d3 59
switches 0:5c4d7b2438d3 60 void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx )
switches 0:5c4d7b2438d3 61 {
switches 0:5c4d7b2438d3 62 mbedtls_mpi_free( &ctx->serial );
switches 0:5c4d7b2438d3 63
switches 0:5c4d7b2438d3 64 mbedtls_asn1_free_named_data_list( &ctx->subject );
switches 0:5c4d7b2438d3 65 mbedtls_asn1_free_named_data_list( &ctx->issuer );
switches 0:5c4d7b2438d3 66 mbedtls_asn1_free_named_data_list( &ctx->extensions );
switches 0:5c4d7b2438d3 67
switches 0:5c4d7b2438d3 68 mbedtls_zeroize( ctx, sizeof(mbedtls_x509write_cert) );
switches 0:5c4d7b2438d3 69 }
switches 0:5c4d7b2438d3 70
switches 0:5c4d7b2438d3 71 void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version )
switches 0:5c4d7b2438d3 72 {
switches 0:5c4d7b2438d3 73 ctx->version = version;
switches 0:5c4d7b2438d3 74 }
switches 0:5c4d7b2438d3 75
switches 0:5c4d7b2438d3 76 void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg )
switches 0:5c4d7b2438d3 77 {
switches 0:5c4d7b2438d3 78 ctx->md_alg = md_alg;
switches 0:5c4d7b2438d3 79 }
switches 0:5c4d7b2438d3 80
switches 0:5c4d7b2438d3 81 void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key )
switches 0:5c4d7b2438d3 82 {
switches 0:5c4d7b2438d3 83 ctx->subject_key = key;
switches 0:5c4d7b2438d3 84 }
switches 0:5c4d7b2438d3 85
switches 0:5c4d7b2438d3 86 void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key )
switches 0:5c4d7b2438d3 87 {
switches 0:5c4d7b2438d3 88 ctx->issuer_key = key;
switches 0:5c4d7b2438d3 89 }
switches 0:5c4d7b2438d3 90
switches 0:5c4d7b2438d3 91 int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
switches 0:5c4d7b2438d3 92 const char *subject_name )
switches 0:5c4d7b2438d3 93 {
switches 0:5c4d7b2438d3 94 return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
switches 0:5c4d7b2438d3 95 }
switches 0:5c4d7b2438d3 96
switches 0:5c4d7b2438d3 97 int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
switches 0:5c4d7b2438d3 98 const char *issuer_name )
switches 0:5c4d7b2438d3 99 {
switches 0:5c4d7b2438d3 100 return mbedtls_x509_string_to_names( &ctx->issuer, issuer_name );
switches 0:5c4d7b2438d3 101 }
switches 0:5c4d7b2438d3 102
switches 0:5c4d7b2438d3 103 int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial )
switches 0:5c4d7b2438d3 104 {
switches 0:5c4d7b2438d3 105 int ret;
switches 0:5c4d7b2438d3 106
switches 0:5c4d7b2438d3 107 if( ( ret = mbedtls_mpi_copy( &ctx->serial, serial ) ) != 0 )
switches 0:5c4d7b2438d3 108 return( ret );
switches 0:5c4d7b2438d3 109
switches 0:5c4d7b2438d3 110 return( 0 );
switches 0:5c4d7b2438d3 111 }
switches 0:5c4d7b2438d3 112
switches 0:5c4d7b2438d3 113 int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
switches 0:5c4d7b2438d3 114 const char *not_after )
switches 0:5c4d7b2438d3 115 {
switches 0:5c4d7b2438d3 116 if( strlen( not_before ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
switches 0:5c4d7b2438d3 117 strlen( not_after ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 )
switches 0:5c4d7b2438d3 118 {
switches 0:5c4d7b2438d3 119 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
switches 0:5c4d7b2438d3 120 }
switches 0:5c4d7b2438d3 121 strncpy( ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
switches 0:5c4d7b2438d3 122 strncpy( ctx->not_after , not_after , MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
switches 0:5c4d7b2438d3 123 ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
switches 0:5c4d7b2438d3 124 ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
switches 0:5c4d7b2438d3 125
switches 0:5c4d7b2438d3 126 return( 0 );
switches 0:5c4d7b2438d3 127 }
switches 0:5c4d7b2438d3 128
switches 0:5c4d7b2438d3 129 int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
switches 0:5c4d7b2438d3 130 const char *oid, size_t oid_len,
switches 0:5c4d7b2438d3 131 int critical,
switches 0:5c4d7b2438d3 132 const unsigned char *val, size_t val_len )
switches 0:5c4d7b2438d3 133 {
switches 0:5c4d7b2438d3 134 return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
switches 0:5c4d7b2438d3 135 critical, val, val_len );
switches 0:5c4d7b2438d3 136 }
switches 0:5c4d7b2438d3 137
switches 0:5c4d7b2438d3 138 int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
switches 0:5c4d7b2438d3 139 int is_ca, int max_pathlen )
switches 0:5c4d7b2438d3 140 {
switches 0:5c4d7b2438d3 141 int ret;
switches 0:5c4d7b2438d3 142 unsigned char buf[9];
switches 0:5c4d7b2438d3 143 unsigned char *c = buf + sizeof(buf);
switches 0:5c4d7b2438d3 144 size_t len = 0;
switches 0:5c4d7b2438d3 145
switches 0:5c4d7b2438d3 146 memset( buf, 0, sizeof(buf) );
switches 0:5c4d7b2438d3 147
switches 0:5c4d7b2438d3 148 if( is_ca && max_pathlen > 127 )
switches 0:5c4d7b2438d3 149 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
switches 0:5c4d7b2438d3 150
switches 0:5c4d7b2438d3 151 if( is_ca )
switches 0:5c4d7b2438d3 152 {
switches 0:5c4d7b2438d3 153 if( max_pathlen >= 0 )
switches 0:5c4d7b2438d3 154 {
switches 0:5c4d7b2438d3 155 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, max_pathlen ) );
switches 0:5c4d7b2438d3 156 }
switches 0:5c4d7b2438d3 157 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( &c, buf, 1 ) );
switches 0:5c4d7b2438d3 158 }
switches 0:5c4d7b2438d3 159
switches 0:5c4d7b2438d3 160 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
switches 0:5c4d7b2438d3 161 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
switches 0:5c4d7b2438d3 162 MBEDTLS_ASN1_SEQUENCE ) );
switches 0:5c4d7b2438d3 163
switches 0:5c4d7b2438d3 164 return mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
switches 0:5c4d7b2438d3 165 MBEDTLS_OID_SIZE( MBEDTLS_OID_BASIC_CONSTRAINTS ),
switches 0:5c4d7b2438d3 166 0, buf + sizeof(buf) - len, len );
switches 0:5c4d7b2438d3 167 }
switches 0:5c4d7b2438d3 168
switches 0:5c4d7b2438d3 169 #if defined(MBEDTLS_SHA1_C)
switches 0:5c4d7b2438d3 170 int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx )
switches 0:5c4d7b2438d3 171 {
switches 0:5c4d7b2438d3 172 int ret;
switches 0:5c4d7b2438d3 173 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
switches 0:5c4d7b2438d3 174 unsigned char *c = buf + sizeof(buf);
switches 0:5c4d7b2438d3 175 size_t len = 0;
switches 0:5c4d7b2438d3 176
switches 0:5c4d7b2438d3 177 memset( buf, 0, sizeof(buf) );
switches 0:5c4d7b2438d3 178 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, ctx->subject_key ) );
switches 0:5c4d7b2438d3 179
switches 0:5c4d7b2438d3 180 mbedtls_sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
switches 0:5c4d7b2438d3 181 c = buf + sizeof(buf) - 20;
switches 0:5c4d7b2438d3 182 len = 20;
switches 0:5c4d7b2438d3 183
switches 0:5c4d7b2438d3 184 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
switches 0:5c4d7b2438d3 185 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_OCTET_STRING ) );
switches 0:5c4d7b2438d3 186
switches 0:5c4d7b2438d3 187 return mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
switches 0:5c4d7b2438d3 188 MBEDTLS_OID_SIZE( MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER ),
switches 0:5c4d7b2438d3 189 0, buf + sizeof(buf) - len, len );
switches 0:5c4d7b2438d3 190 }
switches 0:5c4d7b2438d3 191
switches 0:5c4d7b2438d3 192 int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx )
switches 0:5c4d7b2438d3 193 {
switches 0:5c4d7b2438d3 194 int ret;
switches 0:5c4d7b2438d3 195 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
switches 0:5c4d7b2438d3 196 unsigned char *c = buf + sizeof(buf);
switches 0:5c4d7b2438d3 197 size_t len = 0;
switches 0:5c4d7b2438d3 198
switches 0:5c4d7b2438d3 199 memset( buf, 0, sizeof(buf) );
switches 0:5c4d7b2438d3 200 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, ctx->issuer_key ) );
switches 0:5c4d7b2438d3 201
switches 0:5c4d7b2438d3 202 mbedtls_sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
switches 0:5c4d7b2438d3 203 c = buf + sizeof(buf) - 20;
switches 0:5c4d7b2438d3 204 len = 20;
switches 0:5c4d7b2438d3 205
switches 0:5c4d7b2438d3 206 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
switches 0:5c4d7b2438d3 207 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0 ) );
switches 0:5c4d7b2438d3 208
switches 0:5c4d7b2438d3 209 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
switches 0:5c4d7b2438d3 210 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
switches 0:5c4d7b2438d3 211 MBEDTLS_ASN1_SEQUENCE ) );
switches 0:5c4d7b2438d3 212
switches 0:5c4d7b2438d3 213 return mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
switches 0:5c4d7b2438d3 214 MBEDTLS_OID_SIZE( MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER ),
switches 0:5c4d7b2438d3 215 0, buf + sizeof(buf) - len, len );
switches 0:5c4d7b2438d3 216 }
switches 0:5c4d7b2438d3 217 #endif /* MBEDTLS_SHA1_C */
switches 0:5c4d7b2438d3 218
switches 0:5c4d7b2438d3 219 int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
switches 0:5c4d7b2438d3 220 unsigned int key_usage )
switches 0:5c4d7b2438d3 221 {
switches 0:5c4d7b2438d3 222 unsigned char buf[4], ku;
switches 0:5c4d7b2438d3 223 unsigned char *c;
switches 0:5c4d7b2438d3 224 int ret;
switches 0:5c4d7b2438d3 225
switches 0:5c4d7b2438d3 226 /* We currently only support 7 bits, from 0x80 to 0x02 */
switches 0:5c4d7b2438d3 227 if( ( key_usage & ~0xfe ) != 0 )
switches 0:5c4d7b2438d3 228 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
switches 0:5c4d7b2438d3 229
switches 0:5c4d7b2438d3 230 c = buf + 4;
switches 0:5c4d7b2438d3 231 ku = (unsigned char) key_usage;
switches 0:5c4d7b2438d3 232
switches 0:5c4d7b2438d3 233 if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &ku, 7 ) ) != 4 )
switches 0:5c4d7b2438d3 234 return( ret );
switches 0:5c4d7b2438d3 235
switches 0:5c4d7b2438d3 236 ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
switches 0:5c4d7b2438d3 237 MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
switches 0:5c4d7b2438d3 238 1, buf, 4 );
switches 0:5c4d7b2438d3 239 if( ret != 0 )
switches 0:5c4d7b2438d3 240 return( ret );
switches 0:5c4d7b2438d3 241
switches 0:5c4d7b2438d3 242 return( 0 );
switches 0:5c4d7b2438d3 243 }
switches 0:5c4d7b2438d3 244
switches 0:5c4d7b2438d3 245 int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
switches 0:5c4d7b2438d3 246 unsigned char ns_cert_type )
switches 0:5c4d7b2438d3 247 {
switches 0:5c4d7b2438d3 248 unsigned char buf[4];
switches 0:5c4d7b2438d3 249 unsigned char *c;
switches 0:5c4d7b2438d3 250 int ret;
switches 0:5c4d7b2438d3 251
switches 0:5c4d7b2438d3 252 c = buf + 4;
switches 0:5c4d7b2438d3 253
switches 0:5c4d7b2438d3 254 if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
switches 0:5c4d7b2438d3 255 return( ret );
switches 0:5c4d7b2438d3 256
switches 0:5c4d7b2438d3 257 ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
switches 0:5c4d7b2438d3 258 MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
switches 0:5c4d7b2438d3 259 0, buf, 4 );
switches 0:5c4d7b2438d3 260 if( ret != 0 )
switches 0:5c4d7b2438d3 261 return( ret );
switches 0:5c4d7b2438d3 262
switches 0:5c4d7b2438d3 263 return( 0 );
switches 0:5c4d7b2438d3 264 }
switches 0:5c4d7b2438d3 265
switches 0:5c4d7b2438d3 266 static int x509_write_time( unsigned char **p, unsigned char *start,
switches 0:5c4d7b2438d3 267 const char *time, size_t size )
switches 0:5c4d7b2438d3 268 {
switches 0:5c4d7b2438d3 269 int ret;
switches 0:5c4d7b2438d3 270 size_t len = 0;
switches 0:5c4d7b2438d3 271
switches 0:5c4d7b2438d3 272 /*
switches 0:5c4d7b2438d3 273 * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
switches 0:5c4d7b2438d3 274 */
switches 0:5c4d7b2438d3 275 if( time[0] == '2' && time[1] == '0' && time [2] < '5' )
switches 0:5c4d7b2438d3 276 {
switches 0:5c4d7b2438d3 277 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
switches 0:5c4d7b2438d3 278 (const unsigned char *) time + 2,
switches 0:5c4d7b2438d3 279 size - 2 ) );
switches 0:5c4d7b2438d3 280 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
switches 0:5c4d7b2438d3 281 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_UTC_TIME ) );
switches 0:5c4d7b2438d3 282 }
switches 0:5c4d7b2438d3 283 else
switches 0:5c4d7b2438d3 284 {
switches 0:5c4d7b2438d3 285 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
switches 0:5c4d7b2438d3 286 (const unsigned char *) time,
switches 0:5c4d7b2438d3 287 size ) );
switches 0:5c4d7b2438d3 288 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
switches 0:5c4d7b2438d3 289 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_GENERALIZED_TIME ) );
switches 0:5c4d7b2438d3 290 }
switches 0:5c4d7b2438d3 291
switches 0:5c4d7b2438d3 292 return( (int) len );
switches 0:5c4d7b2438d3 293 }
switches 0:5c4d7b2438d3 294
switches 0:5c4d7b2438d3 295 int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
switches 0:5c4d7b2438d3 296 int (*f_rng)(void *, unsigned char *, size_t),
switches 0:5c4d7b2438d3 297 void *p_rng )
switches 0:5c4d7b2438d3 298 {
switches 0:5c4d7b2438d3 299 int ret;
switches 0:5c4d7b2438d3 300 const char *sig_oid;
switches 0:5c4d7b2438d3 301 size_t sig_oid_len = 0;
switches 0:5c4d7b2438d3 302 unsigned char *c, *c2;
switches 0:5c4d7b2438d3 303 unsigned char hash[64];
switches 0:5c4d7b2438d3 304 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
switches 0:5c4d7b2438d3 305 unsigned char tmp_buf[2048];
switches 0:5c4d7b2438d3 306 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
switches 0:5c4d7b2438d3 307 size_t len = 0;
switches 0:5c4d7b2438d3 308 mbedtls_pk_type_t pk_alg;
switches 0:5c4d7b2438d3 309
switches 0:5c4d7b2438d3 310 /*
switches 0:5c4d7b2438d3 311 * Prepare data to be signed in tmp_buf
switches 0:5c4d7b2438d3 312 */
switches 0:5c4d7b2438d3 313 c = tmp_buf + sizeof( tmp_buf );
switches 0:5c4d7b2438d3 314
switches 0:5c4d7b2438d3 315 /* Signature algorithm needed in TBS, and later for actual signature */
switches 0:5c4d7b2438d3 316 pk_alg = mbedtls_pk_get_type( ctx->issuer_key );
switches 0:5c4d7b2438d3 317 if( pk_alg == MBEDTLS_PK_ECKEY )
switches 0:5c4d7b2438d3 318 pk_alg = MBEDTLS_PK_ECDSA;
switches 0:5c4d7b2438d3 319
switches 0:5c4d7b2438d3 320 if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
switches 0:5c4d7b2438d3 321 &sig_oid, &sig_oid_len ) ) != 0 )
switches 0:5c4d7b2438d3 322 {
switches 0:5c4d7b2438d3 323 return( ret );
switches 0:5c4d7b2438d3 324 }
switches 0:5c4d7b2438d3 325
switches 0:5c4d7b2438d3 326 /*
switches 0:5c4d7b2438d3 327 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
switches 0:5c4d7b2438d3 328 */
switches 0:5c4d7b2438d3 329 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
switches 0:5c4d7b2438d3 330 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
switches 0:5c4d7b2438d3 331 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
switches 0:5c4d7b2438d3 332 MBEDTLS_ASN1_SEQUENCE ) );
switches 0:5c4d7b2438d3 333 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
switches 0:5c4d7b2438d3 334 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC |
switches 0:5c4d7b2438d3 335 MBEDTLS_ASN1_CONSTRUCTED | 3 ) );
switches 0:5c4d7b2438d3 336
switches 0:5c4d7b2438d3 337 /*
switches 0:5c4d7b2438d3 338 * SubjectPublicKeyInfo
switches 0:5c4d7b2438d3 339 */
switches 0:5c4d7b2438d3 340 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->subject_key,
switches 0:5c4d7b2438d3 341 tmp_buf, c - tmp_buf ) );
switches 0:5c4d7b2438d3 342 c -= pub_len;
switches 0:5c4d7b2438d3 343 len += pub_len;
switches 0:5c4d7b2438d3 344
switches 0:5c4d7b2438d3 345 /*
switches 0:5c4d7b2438d3 346 * Subject ::= Name
switches 0:5c4d7b2438d3 347 */
switches 0:5c4d7b2438d3 348 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->subject ) );
switches 0:5c4d7b2438d3 349
switches 0:5c4d7b2438d3 350 /*
switches 0:5c4d7b2438d3 351 * Validity ::= SEQUENCE {
switches 0:5c4d7b2438d3 352 * notBefore Time,
switches 0:5c4d7b2438d3 353 * notAfter Time }
switches 0:5c4d7b2438d3 354 */
switches 0:5c4d7b2438d3 355 sub_len = 0;
switches 0:5c4d7b2438d3 356
switches 0:5c4d7b2438d3 357 MBEDTLS_ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_after,
switches 0:5c4d7b2438d3 358 MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
switches 0:5c4d7b2438d3 359
switches 0:5c4d7b2438d3 360 MBEDTLS_ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_before,
switches 0:5c4d7b2438d3 361 MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
switches 0:5c4d7b2438d3 362
switches 0:5c4d7b2438d3 363 len += sub_len;
switches 0:5c4d7b2438d3 364 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, sub_len ) );
switches 0:5c4d7b2438d3 365 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
switches 0:5c4d7b2438d3 366 MBEDTLS_ASN1_SEQUENCE ) );
switches 0:5c4d7b2438d3 367
switches 0:5c4d7b2438d3 368 /*
switches 0:5c4d7b2438d3 369 * Issuer ::= Name
switches 0:5c4d7b2438d3 370 */
switches 0:5c4d7b2438d3 371 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->issuer ) );
switches 0:5c4d7b2438d3 372
switches 0:5c4d7b2438d3 373 /*
switches 0:5c4d7b2438d3 374 * Signature ::= AlgorithmIdentifier
switches 0:5c4d7b2438d3 375 */
switches 0:5c4d7b2438d3 376 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, tmp_buf,
switches 0:5c4d7b2438d3 377 sig_oid, strlen( sig_oid ), 0 ) );
switches 0:5c4d7b2438d3 378
switches 0:5c4d7b2438d3 379 /*
switches 0:5c4d7b2438d3 380 * Serial ::= INTEGER
switches 0:5c4d7b2438d3 381 */
switches 0:5c4d7b2438d3 382 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, tmp_buf, &ctx->serial ) );
switches 0:5c4d7b2438d3 383
switches 0:5c4d7b2438d3 384 /*
switches 0:5c4d7b2438d3 385 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
switches 0:5c4d7b2438d3 386 */
switches 0:5c4d7b2438d3 387 sub_len = 0;
switches 0:5c4d7b2438d3 388 MBEDTLS_ASN1_CHK_ADD( sub_len, mbedtls_asn1_write_int( &c, tmp_buf, ctx->version ) );
switches 0:5c4d7b2438d3 389 len += sub_len;
switches 0:5c4d7b2438d3 390 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, sub_len ) );
switches 0:5c4d7b2438d3 391 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC |
switches 0:5c4d7b2438d3 392 MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
switches 0:5c4d7b2438d3 393
switches 0:5c4d7b2438d3 394 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
switches 0:5c4d7b2438d3 395 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
switches 0:5c4d7b2438d3 396 MBEDTLS_ASN1_SEQUENCE ) );
switches 0:5c4d7b2438d3 397
switches 0:5c4d7b2438d3 398 /*
switches 0:5c4d7b2438d3 399 * Make signature
switches 0:5c4d7b2438d3 400 */
switches 0:5c4d7b2438d3 401 mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
switches 0:5c4d7b2438d3 402
switches 0:5c4d7b2438d3 403 if( ( ret = mbedtls_pk_sign( ctx->issuer_key, ctx->md_alg, hash, 0, sig, &sig_len,
switches 0:5c4d7b2438d3 404 f_rng, p_rng ) ) != 0 )
switches 0:5c4d7b2438d3 405 {
switches 0:5c4d7b2438d3 406 return( ret );
switches 0:5c4d7b2438d3 407 }
switches 0:5c4d7b2438d3 408
switches 0:5c4d7b2438d3 409 /*
switches 0:5c4d7b2438d3 410 * Write data to output buffer
switches 0:5c4d7b2438d3 411 */
switches 0:5c4d7b2438d3 412 c2 = buf + size;
switches 0:5c4d7b2438d3 413 MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, buf,
switches 0:5c4d7b2438d3 414 sig_oid, sig_oid_len, sig, sig_len ) );
switches 0:5c4d7b2438d3 415
switches 0:5c4d7b2438d3 416 if( len > (size_t)( c2 - buf ) )
switches 0:5c4d7b2438d3 417 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
switches 0:5c4d7b2438d3 418
switches 0:5c4d7b2438d3 419 c2 -= len;
switches 0:5c4d7b2438d3 420 memcpy( c2, c, len );
switches 0:5c4d7b2438d3 421
switches 0:5c4d7b2438d3 422 len += sig_and_oid_len;
switches 0:5c4d7b2438d3 423 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
switches 0:5c4d7b2438d3 424 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, MBEDTLS_ASN1_CONSTRUCTED |
switches 0:5c4d7b2438d3 425 MBEDTLS_ASN1_SEQUENCE ) );
switches 0:5c4d7b2438d3 426
switches 0:5c4d7b2438d3 427 return( (int) len );
switches 0:5c4d7b2438d3 428 }
switches 0:5c4d7b2438d3 429
switches 0:5c4d7b2438d3 430 #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
switches 0:5c4d7b2438d3 431 #define PEM_END_CRT "-----END CERTIFICATE-----\n"
switches 0:5c4d7b2438d3 432
switches 0:5c4d7b2438d3 433 #if defined(MBEDTLS_PEM_WRITE_C)
switches 0:5c4d7b2438d3 434 int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *crt, unsigned char *buf, size_t size,
switches 0:5c4d7b2438d3 435 int (*f_rng)(void *, unsigned char *, size_t),
switches 0:5c4d7b2438d3 436 void *p_rng )
switches 0:5c4d7b2438d3 437 {
switches 0:5c4d7b2438d3 438 int ret;
switches 0:5c4d7b2438d3 439 unsigned char output_buf[4096];
switches 0:5c4d7b2438d3 440 size_t olen = 0;
switches 0:5c4d7b2438d3 441
switches 0:5c4d7b2438d3 442 if( ( ret = mbedtls_x509write_crt_der( crt, output_buf, sizeof(output_buf),
switches 0:5c4d7b2438d3 443 f_rng, p_rng ) ) < 0 )
switches 0:5c4d7b2438d3 444 {
switches 0:5c4d7b2438d3 445 return( ret );
switches 0:5c4d7b2438d3 446 }
switches 0:5c4d7b2438d3 447
switches 0:5c4d7b2438d3 448 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
switches 0:5c4d7b2438d3 449 output_buf + sizeof(output_buf) - ret,
switches 0:5c4d7b2438d3 450 ret, buf, size, &olen ) ) != 0 )
switches 0:5c4d7b2438d3 451 {
switches 0:5c4d7b2438d3 452 return( ret );
switches 0:5c4d7b2438d3 453 }
switches 0:5c4d7b2438d3 454
switches 0:5c4d7b2438d3 455 return( 0 );
switches 0:5c4d7b2438d3 456 }
switches 0:5c4d7b2438d3 457 #endif /* MBEDTLS_PEM_WRITE_C */
switches 0:5c4d7b2438d3 458
switches 0:5c4d7b2438d3 459 #endif /* MBEDTLS_X509_CRT_WRITE_C */