Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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