mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Committer:
Brian Daniels
Date:
Thu Apr 07 11:11:18 2016 +0100
Revision:
4:bef26f687287
Parent:
1:24750b9ad5ef
Adding ported selftest test case

Who changed what in which revision?

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