mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Committer:
Christopher Haster
Date:
Fri Jan 22 16:44:49 2016 -0600
Revision:
1:24750b9ad5ef
Initial move of mbedtls to mercurial

Who changed what in which revision?

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