Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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