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