mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

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

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

Who changed what in which revision?

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