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.

Committer:
JMF
Date:
Fri Mar 24 22:26:23 2017 +0000
Revision:
29:b278b745fb4f
Parent:
12:0071cb144c7a
updated Class name of TCPSocketConnection to WncTCPSocketConnection;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 12:0071cb144c7a 1 /*
JMF 12:0071cb144c7a 2 * X.509 Certificate Signing Request 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 * - CSRs: PKCS#10 v1.7 aka RFC 2986
JMF 12:0071cb144c7a 24 * - attributes: PKCS#9 v2.0 aka RFC 2985
JMF 12:0071cb144c7a 25 */
JMF 12:0071cb144c7a 26
JMF 12:0071cb144c7a 27 #if !defined(MBEDTLS_CONFIG_FILE)
JMF 12:0071cb144c7a 28 #include "mbedtls/config.h"
JMF 12:0071cb144c7a 29 #else
JMF 12:0071cb144c7a 30 #include MBEDTLS_CONFIG_FILE
JMF 12:0071cb144c7a 31 #endif
JMF 12:0071cb144c7a 32
JMF 12:0071cb144c7a 33 #if defined(MBEDTLS_X509_CSR_WRITE_C)
JMF 12:0071cb144c7a 34
JMF 12:0071cb144c7a 35 #include "mbedtls/x509_csr.h"
JMF 12:0071cb144c7a 36 #include "mbedtls/oid.h"
JMF 12:0071cb144c7a 37 #include "mbedtls/asn1write.h"
JMF 12:0071cb144c7a 38
JMF 12:0071cb144c7a 39 #include <string.h>
JMF 12:0071cb144c7a 40 #include <stdlib.h>
JMF 12:0071cb144c7a 41
JMF 12:0071cb144c7a 42 #if defined(MBEDTLS_PEM_WRITE_C)
JMF 12:0071cb144c7a 43 #include "mbedtls/pem.h"
JMF 12:0071cb144c7a 44 #endif
JMF 12:0071cb144c7a 45
JMF 12:0071cb144c7a 46 /* Implementation that should never be optimized out by the compiler */
JMF 12:0071cb144c7a 47 static void mbedtls_zeroize( void *v, size_t n ) {
JMF 12:0071cb144c7a 48 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
JMF 12:0071cb144c7a 49 }
JMF 12:0071cb144c7a 50
JMF 12:0071cb144c7a 51 void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx )
JMF 12:0071cb144c7a 52 {
JMF 12:0071cb144c7a 53 memset( ctx, 0, sizeof(mbedtls_x509write_csr) );
JMF 12:0071cb144c7a 54 }
JMF 12:0071cb144c7a 55
JMF 12:0071cb144c7a 56 void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx )
JMF 12:0071cb144c7a 57 {
JMF 12:0071cb144c7a 58 mbedtls_asn1_free_named_data_list( &ctx->subject );
JMF 12:0071cb144c7a 59 mbedtls_asn1_free_named_data_list( &ctx->extensions );
JMF 12:0071cb144c7a 60
JMF 12:0071cb144c7a 61 mbedtls_zeroize( ctx, sizeof(mbedtls_x509write_csr) );
JMF 12:0071cb144c7a 62 }
JMF 12:0071cb144c7a 63
JMF 12:0071cb144c7a 64 void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg )
JMF 12:0071cb144c7a 65 {
JMF 12:0071cb144c7a 66 ctx->md_alg = md_alg;
JMF 12:0071cb144c7a 67 }
JMF 12:0071cb144c7a 68
JMF 12:0071cb144c7a 69 void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key )
JMF 12:0071cb144c7a 70 {
JMF 12:0071cb144c7a 71 ctx->key = key;
JMF 12:0071cb144c7a 72 }
JMF 12:0071cb144c7a 73
JMF 12:0071cb144c7a 74 int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
JMF 12:0071cb144c7a 75 const char *subject_name )
JMF 12:0071cb144c7a 76 {
JMF 12:0071cb144c7a 77 return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
JMF 12:0071cb144c7a 78 }
JMF 12:0071cb144c7a 79
JMF 12:0071cb144c7a 80 int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
JMF 12:0071cb144c7a 81 const char *oid, size_t oid_len,
JMF 12:0071cb144c7a 82 const unsigned char *val, size_t val_len )
JMF 12:0071cb144c7a 83 {
JMF 12:0071cb144c7a 84 return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
JMF 12:0071cb144c7a 85 0, val, val_len );
JMF 12:0071cb144c7a 86 }
JMF 12:0071cb144c7a 87
JMF 12:0071cb144c7a 88 int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage )
JMF 12:0071cb144c7a 89 {
JMF 12:0071cb144c7a 90 unsigned char buf[4];
JMF 12:0071cb144c7a 91 unsigned char *c;
JMF 12:0071cb144c7a 92 int ret;
JMF 12:0071cb144c7a 93
JMF 12:0071cb144c7a 94 c = buf + 4;
JMF 12:0071cb144c7a 95
JMF 12:0071cb144c7a 96 if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
JMF 12:0071cb144c7a 97 return( ret );
JMF 12:0071cb144c7a 98
JMF 12:0071cb144c7a 99 ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
JMF 12:0071cb144c7a 100 MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
JMF 12:0071cb144c7a 101 buf, 4 );
JMF 12:0071cb144c7a 102 if( ret != 0 )
JMF 12:0071cb144c7a 103 return( ret );
JMF 12:0071cb144c7a 104
JMF 12:0071cb144c7a 105 return( 0 );
JMF 12:0071cb144c7a 106 }
JMF 12:0071cb144c7a 107
JMF 12:0071cb144c7a 108 int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
JMF 12:0071cb144c7a 109 unsigned char ns_cert_type )
JMF 12:0071cb144c7a 110 {
JMF 12:0071cb144c7a 111 unsigned char buf[4];
JMF 12:0071cb144c7a 112 unsigned char *c;
JMF 12:0071cb144c7a 113 int ret;
JMF 12:0071cb144c7a 114
JMF 12:0071cb144c7a 115 c = buf + 4;
JMF 12:0071cb144c7a 116
JMF 12:0071cb144c7a 117 if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
JMF 12:0071cb144c7a 118 return( ret );
JMF 12:0071cb144c7a 119
JMF 12:0071cb144c7a 120 ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
JMF 12:0071cb144c7a 121 MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
JMF 12:0071cb144c7a 122 buf, 4 );
JMF 12:0071cb144c7a 123 if( ret != 0 )
JMF 12:0071cb144c7a 124 return( ret );
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_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
JMF 12:0071cb144c7a 130 int (*f_rng)(void *, unsigned char *, size_t),
JMF 12:0071cb144c7a 131 void *p_rng )
JMF 12:0071cb144c7a 132 {
JMF 12:0071cb144c7a 133 int ret;
JMF 12:0071cb144c7a 134 const char *sig_oid;
JMF 12:0071cb144c7a 135 size_t sig_oid_len = 0;
JMF 12:0071cb144c7a 136 unsigned char *c, *c2;
JMF 12:0071cb144c7a 137 unsigned char hash[64];
JMF 12:0071cb144c7a 138 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
JMF 12:0071cb144c7a 139 unsigned char tmp_buf[2048];
JMF 12:0071cb144c7a 140 size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
JMF 12:0071cb144c7a 141 size_t len = 0;
JMF 12:0071cb144c7a 142 mbedtls_pk_type_t pk_alg;
JMF 12:0071cb144c7a 143
JMF 12:0071cb144c7a 144 /*
JMF 12:0071cb144c7a 145 * Prepare data to be signed in tmp_buf
JMF 12:0071cb144c7a 146 */
JMF 12:0071cb144c7a 147 c = tmp_buf + sizeof( tmp_buf );
JMF 12:0071cb144c7a 148
JMF 12:0071cb144c7a 149 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
JMF 12:0071cb144c7a 150
JMF 12:0071cb144c7a 151 if( len )
JMF 12:0071cb144c7a 152 {
JMF 12:0071cb144c7a 153 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
JMF 12:0071cb144c7a 154 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
JMF 12:0071cb144c7a 155 MBEDTLS_ASN1_SEQUENCE ) );
JMF 12:0071cb144c7a 156
JMF 12:0071cb144c7a 157 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
JMF 12:0071cb144c7a 158 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
JMF 12:0071cb144c7a 159 MBEDTLS_ASN1_SET ) );
JMF 12:0071cb144c7a 160
JMF 12:0071cb144c7a 161 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( &c, tmp_buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
JMF 12:0071cb144c7a 162 MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_CSR_EXT_REQ ) ) );
JMF 12:0071cb144c7a 163
JMF 12:0071cb144c7a 164 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
JMF 12:0071cb144c7a 165 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
JMF 12:0071cb144c7a 166 MBEDTLS_ASN1_SEQUENCE ) );
JMF 12:0071cb144c7a 167 }
JMF 12:0071cb144c7a 168
JMF 12:0071cb144c7a 169 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
JMF 12:0071cb144c7a 170 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
JMF 12:0071cb144c7a 171 MBEDTLS_ASN1_CONTEXT_SPECIFIC ) );
JMF 12:0071cb144c7a 172
JMF 12:0071cb144c7a 173 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->key,
JMF 12:0071cb144c7a 174 tmp_buf, c - tmp_buf ) );
JMF 12:0071cb144c7a 175 c -= pub_len;
JMF 12:0071cb144c7a 176 len += pub_len;
JMF 12:0071cb144c7a 177
JMF 12:0071cb144c7a 178 /*
JMF 12:0071cb144c7a 179 * Subject ::= Name
JMF 12:0071cb144c7a 180 */
JMF 12:0071cb144c7a 181 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->subject ) );
JMF 12:0071cb144c7a 182
JMF 12:0071cb144c7a 183 /*
JMF 12:0071cb144c7a 184 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
JMF 12:0071cb144c7a 185 */
JMF 12:0071cb144c7a 186 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, tmp_buf, 0 ) );
JMF 12:0071cb144c7a 187
JMF 12:0071cb144c7a 188 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
JMF 12:0071cb144c7a 189 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
JMF 12:0071cb144c7a 190 MBEDTLS_ASN1_SEQUENCE ) );
JMF 12:0071cb144c7a 191
JMF 12:0071cb144c7a 192 /*
JMF 12:0071cb144c7a 193 * Prepare signature
JMF 12:0071cb144c7a 194 */
JMF 12:0071cb144c7a 195 mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
JMF 12:0071cb144c7a 196
JMF 12:0071cb144c7a 197 pk_alg = mbedtls_pk_get_type( ctx->key );
JMF 12:0071cb144c7a 198 if( pk_alg == MBEDTLS_PK_ECKEY )
JMF 12:0071cb144c7a 199 pk_alg = MBEDTLS_PK_ECDSA;
JMF 12:0071cb144c7a 200
JMF 12:0071cb144c7a 201 if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
JMF 12:0071cb144c7a 202 f_rng, p_rng ) ) != 0 ||
JMF 12:0071cb144c7a 203 ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
JMF 12:0071cb144c7a 204 &sig_oid, &sig_oid_len ) ) != 0 )
JMF 12:0071cb144c7a 205 {
JMF 12:0071cb144c7a 206 return( ret );
JMF 12:0071cb144c7a 207 }
JMF 12:0071cb144c7a 208
JMF 12:0071cb144c7a 209 /*
JMF 12:0071cb144c7a 210 * Write data to output buffer
JMF 12:0071cb144c7a 211 */
JMF 12:0071cb144c7a 212 c2 = buf + size;
JMF 12:0071cb144c7a 213 MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, buf,
JMF 12:0071cb144c7a 214 sig_oid, sig_oid_len, sig, sig_len ) );
JMF 12:0071cb144c7a 215
JMF 12:0071cb144c7a 216 c2 -= len;
JMF 12:0071cb144c7a 217 memcpy( c2, c, len );
JMF 12:0071cb144c7a 218
JMF 12:0071cb144c7a 219 len += sig_and_oid_len;
JMF 12:0071cb144c7a 220 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
JMF 12:0071cb144c7a 221 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, MBEDTLS_ASN1_CONSTRUCTED |
JMF 12:0071cb144c7a 222 MBEDTLS_ASN1_SEQUENCE ) );
JMF 12:0071cb144c7a 223
JMF 12:0071cb144c7a 224 return( (int) len );
JMF 12:0071cb144c7a 225 }
JMF 12:0071cb144c7a 226
JMF 12:0071cb144c7a 227 #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
JMF 12:0071cb144c7a 228 #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
JMF 12:0071cb144c7a 229
JMF 12:0071cb144c7a 230 #if defined(MBEDTLS_PEM_WRITE_C)
JMF 12:0071cb144c7a 231 int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
JMF 12:0071cb144c7a 232 int (*f_rng)(void *, unsigned char *, size_t),
JMF 12:0071cb144c7a 233 void *p_rng )
JMF 12:0071cb144c7a 234 {
JMF 12:0071cb144c7a 235 int ret;
JMF 12:0071cb144c7a 236 unsigned char output_buf[4096];
JMF 12:0071cb144c7a 237 size_t olen = 0;
JMF 12:0071cb144c7a 238
JMF 12:0071cb144c7a 239 if( ( ret = mbedtls_x509write_csr_der( ctx, output_buf, sizeof(output_buf),
JMF 12:0071cb144c7a 240 f_rng, p_rng ) ) < 0 )
JMF 12:0071cb144c7a 241 {
JMF 12:0071cb144c7a 242 return( ret );
JMF 12:0071cb144c7a 243 }
JMF 12:0071cb144c7a 244
JMF 12:0071cb144c7a 245 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
JMF 12:0071cb144c7a 246 output_buf + sizeof(output_buf) - ret,
JMF 12:0071cb144c7a 247 ret, buf, size, &olen ) ) != 0 )
JMF 12:0071cb144c7a 248 {
JMF 12:0071cb144c7a 249 return( ret );
JMF 12:0071cb144c7a 250 }
JMF 12:0071cb144c7a 251
JMF 12:0071cb144c7a 252 return( 0 );
JMF 12:0071cb144c7a 253 }
JMF 12:0071cb144c7a 254 #endif /* MBEDTLS_PEM_WRITE_C */
JMF 12:0071cb144c7a 255
JMF 12:0071cb144c7a 256 #endif /* MBEDTLS_X509_CSR_WRITE_C */