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