Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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