mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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