This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
maclobdell
Date:
Wed May 18 19:06:32 2016 +0000
Revision:
0:f7c60d3e7b8a
clean version

Who changed what in which revision?

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