mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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