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