Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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