Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/features/mbedtls/src/x509write_crt.c@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | /* |
marcozecchini | 0:9fca2b23d0ba | 2 | * X.509 certificate 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 | * - certificates: RFC 5280, updated by RFC 6818 |
marcozecchini | 0:9fca2b23d0ba | 24 | * - CSRs: PKCS#10 v1.7 aka RFC 2986 |
marcozecchini | 0:9fca2b23d0ba | 25 | * - attributes: PKCS#9 v2.0 aka RFC 2985 |
marcozecchini | 0:9fca2b23d0ba | 26 | */ |
marcozecchini | 0:9fca2b23d0ba | 27 | |
marcozecchini | 0:9fca2b23d0ba | 28 | #if !defined(MBEDTLS_CONFIG_FILE) |
marcozecchini | 0:9fca2b23d0ba | 29 | #include "mbedtls/config.h" |
marcozecchini | 0:9fca2b23d0ba | 30 | #else |
marcozecchini | 0:9fca2b23d0ba | 31 | #include MBEDTLS_CONFIG_FILE |
marcozecchini | 0:9fca2b23d0ba | 32 | #endif |
marcozecchini | 0:9fca2b23d0ba | 33 | |
marcozecchini | 0:9fca2b23d0ba | 34 | #if defined(MBEDTLS_X509_CRT_WRITE_C) |
marcozecchini | 0:9fca2b23d0ba | 35 | |
marcozecchini | 0:9fca2b23d0ba | 36 | #include "mbedtls/x509_crt.h" |
marcozecchini | 0:9fca2b23d0ba | 37 | #include "mbedtls/oid.h" |
marcozecchini | 0:9fca2b23d0ba | 38 | #include "mbedtls/asn1write.h" |
marcozecchini | 0:9fca2b23d0ba | 39 | #include "mbedtls/sha1.h" |
marcozecchini | 0:9fca2b23d0ba | 40 | |
marcozecchini | 0:9fca2b23d0ba | 41 | #include <string.h> |
marcozecchini | 0:9fca2b23d0ba | 42 | |
marcozecchini | 0:9fca2b23d0ba | 43 | #if defined(MBEDTLS_PEM_WRITE_C) |
marcozecchini | 0:9fca2b23d0ba | 44 | #include "mbedtls/pem.h" |
marcozecchini | 0:9fca2b23d0ba | 45 | #endif /* MBEDTLS_PEM_WRITE_C */ |
marcozecchini | 0:9fca2b23d0ba | 46 | |
marcozecchini | 0:9fca2b23d0ba | 47 | /* Implementation that should never be optimized out by the compiler */ |
marcozecchini | 0:9fca2b23d0ba | 48 | static void mbedtls_zeroize( void *v, size_t n ) { |
marcozecchini | 0:9fca2b23d0ba | 49 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
marcozecchini | 0:9fca2b23d0ba | 50 | } |
marcozecchini | 0:9fca2b23d0ba | 51 | |
marcozecchini | 0:9fca2b23d0ba | 52 | void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx ) |
marcozecchini | 0:9fca2b23d0ba | 53 | { |
marcozecchini | 0:9fca2b23d0ba | 54 | memset( ctx, 0, sizeof(mbedtls_x509write_cert) ); |
marcozecchini | 0:9fca2b23d0ba | 55 | |
marcozecchini | 0:9fca2b23d0ba | 56 | mbedtls_mpi_init( &ctx->serial ); |
marcozecchini | 0:9fca2b23d0ba | 57 | ctx->version = MBEDTLS_X509_CRT_VERSION_3; |
marcozecchini | 0:9fca2b23d0ba | 58 | } |
marcozecchini | 0:9fca2b23d0ba | 59 | |
marcozecchini | 0:9fca2b23d0ba | 60 | void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx ) |
marcozecchini | 0:9fca2b23d0ba | 61 | { |
marcozecchini | 0:9fca2b23d0ba | 62 | mbedtls_mpi_free( &ctx->serial ); |
marcozecchini | 0:9fca2b23d0ba | 63 | |
marcozecchini | 0:9fca2b23d0ba | 64 | mbedtls_asn1_free_named_data_list( &ctx->subject ); |
marcozecchini | 0:9fca2b23d0ba | 65 | mbedtls_asn1_free_named_data_list( &ctx->issuer ); |
marcozecchini | 0:9fca2b23d0ba | 66 | mbedtls_asn1_free_named_data_list( &ctx->extensions ); |
marcozecchini | 0:9fca2b23d0ba | 67 | |
marcozecchini | 0:9fca2b23d0ba | 68 | mbedtls_zeroize( ctx, sizeof(mbedtls_x509write_cert) ); |
marcozecchini | 0:9fca2b23d0ba | 69 | } |
marcozecchini | 0:9fca2b23d0ba | 70 | |
marcozecchini | 0:9fca2b23d0ba | 71 | void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version ) |
marcozecchini | 0:9fca2b23d0ba | 72 | { |
marcozecchini | 0:9fca2b23d0ba | 73 | ctx->version = version; |
marcozecchini | 0:9fca2b23d0ba | 74 | } |
marcozecchini | 0:9fca2b23d0ba | 75 | |
marcozecchini | 0:9fca2b23d0ba | 76 | void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg ) |
marcozecchini | 0:9fca2b23d0ba | 77 | { |
marcozecchini | 0:9fca2b23d0ba | 78 | ctx->md_alg = md_alg; |
marcozecchini | 0:9fca2b23d0ba | 79 | } |
marcozecchini | 0:9fca2b23d0ba | 80 | |
marcozecchini | 0:9fca2b23d0ba | 81 | void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key ) |
marcozecchini | 0:9fca2b23d0ba | 82 | { |
marcozecchini | 0:9fca2b23d0ba | 83 | ctx->subject_key = key; |
marcozecchini | 0:9fca2b23d0ba | 84 | } |
marcozecchini | 0:9fca2b23d0ba | 85 | |
marcozecchini | 0:9fca2b23d0ba | 86 | void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key ) |
marcozecchini | 0:9fca2b23d0ba | 87 | { |
marcozecchini | 0:9fca2b23d0ba | 88 | ctx->issuer_key = key; |
marcozecchini | 0:9fca2b23d0ba | 89 | } |
marcozecchini | 0:9fca2b23d0ba | 90 | |
marcozecchini | 0:9fca2b23d0ba | 91 | int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx, |
marcozecchini | 0:9fca2b23d0ba | 92 | const char *subject_name ) |
marcozecchini | 0:9fca2b23d0ba | 93 | { |
marcozecchini | 0:9fca2b23d0ba | 94 | return mbedtls_x509_string_to_names( &ctx->subject, subject_name ); |
marcozecchini | 0:9fca2b23d0ba | 95 | } |
marcozecchini | 0:9fca2b23d0ba | 96 | |
marcozecchini | 0:9fca2b23d0ba | 97 | int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx, |
marcozecchini | 0:9fca2b23d0ba | 98 | const char *issuer_name ) |
marcozecchini | 0:9fca2b23d0ba | 99 | { |
marcozecchini | 0:9fca2b23d0ba | 100 | return mbedtls_x509_string_to_names( &ctx->issuer, issuer_name ); |
marcozecchini | 0:9fca2b23d0ba | 101 | } |
marcozecchini | 0:9fca2b23d0ba | 102 | |
marcozecchini | 0:9fca2b23d0ba | 103 | int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial ) |
marcozecchini | 0:9fca2b23d0ba | 104 | { |
marcozecchini | 0:9fca2b23d0ba | 105 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 106 | |
marcozecchini | 0:9fca2b23d0ba | 107 | if( ( ret = mbedtls_mpi_copy( &ctx->serial, serial ) ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 108 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 109 | |
marcozecchini | 0:9fca2b23d0ba | 110 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 111 | } |
marcozecchini | 0:9fca2b23d0ba | 112 | |
marcozecchini | 0:9fca2b23d0ba | 113 | int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before, |
marcozecchini | 0:9fca2b23d0ba | 114 | const char *not_after ) |
marcozecchini | 0:9fca2b23d0ba | 115 | { |
marcozecchini | 0:9fca2b23d0ba | 116 | if( strlen( not_before ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 || |
marcozecchini | 0:9fca2b23d0ba | 117 | strlen( not_after ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ) |
marcozecchini | 0:9fca2b23d0ba | 118 | { |
marcozecchini | 0:9fca2b23d0ba | 119 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
marcozecchini | 0:9fca2b23d0ba | 120 | } |
marcozecchini | 0:9fca2b23d0ba | 121 | strncpy( ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN ); |
marcozecchini | 0:9fca2b23d0ba | 122 | strncpy( ctx->not_after , not_after , MBEDTLS_X509_RFC5280_UTC_TIME_LEN ); |
marcozecchini | 0:9fca2b23d0ba | 123 | ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z'; |
marcozecchini | 0:9fca2b23d0ba | 124 | ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z'; |
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_crt_set_extension( mbedtls_x509write_cert *ctx, |
marcozecchini | 0:9fca2b23d0ba | 130 | const char *oid, size_t oid_len, |
marcozecchini | 0:9fca2b23d0ba | 131 | int critical, |
marcozecchini | 0:9fca2b23d0ba | 132 | const unsigned char *val, size_t val_len ) |
marcozecchini | 0:9fca2b23d0ba | 133 | { |
marcozecchini | 0:9fca2b23d0ba | 134 | return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len, |
marcozecchini | 0:9fca2b23d0ba | 135 | critical, val, val_len ); |
marcozecchini | 0:9fca2b23d0ba | 136 | } |
marcozecchini | 0:9fca2b23d0ba | 137 | |
marcozecchini | 0:9fca2b23d0ba | 138 | int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx, |
marcozecchini | 0:9fca2b23d0ba | 139 | int is_ca, int max_pathlen ) |
marcozecchini | 0:9fca2b23d0ba | 140 | { |
marcozecchini | 0:9fca2b23d0ba | 141 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 142 | unsigned char buf[9]; |
marcozecchini | 0:9fca2b23d0ba | 143 | unsigned char *c = buf + sizeof(buf); |
marcozecchini | 0:9fca2b23d0ba | 144 | size_t len = 0; |
marcozecchini | 0:9fca2b23d0ba | 145 | |
marcozecchini | 0:9fca2b23d0ba | 146 | memset( buf, 0, sizeof(buf) ); |
marcozecchini | 0:9fca2b23d0ba | 147 | |
marcozecchini | 0:9fca2b23d0ba | 148 | if( is_ca && max_pathlen > 127 ) |
marcozecchini | 0:9fca2b23d0ba | 149 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
marcozecchini | 0:9fca2b23d0ba | 150 | |
marcozecchini | 0:9fca2b23d0ba | 151 | if( is_ca ) |
marcozecchini | 0:9fca2b23d0ba | 152 | { |
marcozecchini | 0:9fca2b23d0ba | 153 | if( max_pathlen >= 0 ) |
marcozecchini | 0:9fca2b23d0ba | 154 | { |
marcozecchini | 0:9fca2b23d0ba | 155 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, max_pathlen ) ); |
marcozecchini | 0:9fca2b23d0ba | 156 | } |
marcozecchini | 0:9fca2b23d0ba | 157 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( &c, buf, 1 ) ); |
marcozecchini | 0:9fca2b23d0ba | 158 | } |
marcozecchini | 0:9fca2b23d0ba | 159 | |
marcozecchini | 0:9fca2b23d0ba | 160 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
marcozecchini | 0:9fca2b23d0ba | 161 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED | |
marcozecchini | 0:9fca2b23d0ba | 162 | MBEDTLS_ASN1_SEQUENCE ) ); |
marcozecchini | 0:9fca2b23d0ba | 163 | |
marcozecchini | 0:9fca2b23d0ba | 164 | return mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_BASIC_CONSTRAINTS, |
marcozecchini | 0:9fca2b23d0ba | 165 | MBEDTLS_OID_SIZE( MBEDTLS_OID_BASIC_CONSTRAINTS ), |
marcozecchini | 0:9fca2b23d0ba | 166 | 0, buf + sizeof(buf) - len, len ); |
marcozecchini | 0:9fca2b23d0ba | 167 | } |
marcozecchini | 0:9fca2b23d0ba | 168 | |
marcozecchini | 0:9fca2b23d0ba | 169 | #if defined(MBEDTLS_SHA1_C) |
marcozecchini | 0:9fca2b23d0ba | 170 | int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx ) |
marcozecchini | 0:9fca2b23d0ba | 171 | { |
marcozecchini | 0:9fca2b23d0ba | 172 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 173 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */ |
marcozecchini | 0:9fca2b23d0ba | 174 | unsigned char *c = buf + sizeof(buf); |
marcozecchini | 0:9fca2b23d0ba | 175 | size_t len = 0; |
marcozecchini | 0:9fca2b23d0ba | 176 | |
marcozecchini | 0:9fca2b23d0ba | 177 | memset( buf, 0, sizeof(buf) ); |
marcozecchini | 0:9fca2b23d0ba | 178 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, ctx->subject_key ) ); |
marcozecchini | 0:9fca2b23d0ba | 179 | |
marcozecchini | 0:9fca2b23d0ba | 180 | mbedtls_sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 ); |
marcozecchini | 0:9fca2b23d0ba | 181 | c = buf + sizeof(buf) - 20; |
marcozecchini | 0:9fca2b23d0ba | 182 | len = 20; |
marcozecchini | 0:9fca2b23d0ba | 183 | |
marcozecchini | 0:9fca2b23d0ba | 184 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
marcozecchini | 0:9fca2b23d0ba | 185 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_OCTET_STRING ) ); |
marcozecchini | 0:9fca2b23d0ba | 186 | |
marcozecchini | 0:9fca2b23d0ba | 187 | return mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER, |
marcozecchini | 0:9fca2b23d0ba | 188 | MBEDTLS_OID_SIZE( MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER ), |
marcozecchini | 0:9fca2b23d0ba | 189 | 0, buf + sizeof(buf) - len, len ); |
marcozecchini | 0:9fca2b23d0ba | 190 | } |
marcozecchini | 0:9fca2b23d0ba | 191 | |
marcozecchini | 0:9fca2b23d0ba | 192 | int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx ) |
marcozecchini | 0:9fca2b23d0ba | 193 | { |
marcozecchini | 0:9fca2b23d0ba | 194 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 195 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */ |
marcozecchini | 0:9fca2b23d0ba | 196 | unsigned char *c = buf + sizeof(buf); |
marcozecchini | 0:9fca2b23d0ba | 197 | size_t len = 0; |
marcozecchini | 0:9fca2b23d0ba | 198 | |
marcozecchini | 0:9fca2b23d0ba | 199 | memset( buf, 0, sizeof(buf) ); |
marcozecchini | 0:9fca2b23d0ba | 200 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, ctx->issuer_key ) ); |
marcozecchini | 0:9fca2b23d0ba | 201 | |
marcozecchini | 0:9fca2b23d0ba | 202 | mbedtls_sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 ); |
marcozecchini | 0:9fca2b23d0ba | 203 | c = buf + sizeof(buf) - 20; |
marcozecchini | 0:9fca2b23d0ba | 204 | len = 20; |
marcozecchini | 0:9fca2b23d0ba | 205 | |
marcozecchini | 0:9fca2b23d0ba | 206 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
marcozecchini | 0:9fca2b23d0ba | 207 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0 ) ); |
marcozecchini | 0:9fca2b23d0ba | 208 | |
marcozecchini | 0:9fca2b23d0ba | 209 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
marcozecchini | 0:9fca2b23d0ba | 210 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED | |
marcozecchini | 0:9fca2b23d0ba | 211 | MBEDTLS_ASN1_SEQUENCE ) ); |
marcozecchini | 0:9fca2b23d0ba | 212 | |
marcozecchini | 0:9fca2b23d0ba | 213 | return mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER, |
marcozecchini | 0:9fca2b23d0ba | 214 | MBEDTLS_OID_SIZE( MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER ), |
marcozecchini | 0:9fca2b23d0ba | 215 | 0, buf + sizeof(buf) - len, len ); |
marcozecchini | 0:9fca2b23d0ba | 216 | } |
marcozecchini | 0:9fca2b23d0ba | 217 | #endif /* MBEDTLS_SHA1_C */ |
marcozecchini | 0:9fca2b23d0ba | 218 | |
marcozecchini | 0:9fca2b23d0ba | 219 | int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx, |
marcozecchini | 0:9fca2b23d0ba | 220 | unsigned int key_usage ) |
marcozecchini | 0:9fca2b23d0ba | 221 | { |
marcozecchini | 0:9fca2b23d0ba | 222 | unsigned char buf[4], ku; |
marcozecchini | 0:9fca2b23d0ba | 223 | unsigned char *c; |
marcozecchini | 0:9fca2b23d0ba | 224 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 225 | |
marcozecchini | 0:9fca2b23d0ba | 226 | /* We currently only support 7 bits, from 0x80 to 0x02 */ |
marcozecchini | 0:9fca2b23d0ba | 227 | if( ( key_usage & ~0xfe ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 228 | return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ); |
marcozecchini | 0:9fca2b23d0ba | 229 | |
marcozecchini | 0:9fca2b23d0ba | 230 | c = buf + 4; |
marcozecchini | 0:9fca2b23d0ba | 231 | ku = (unsigned char) key_usage; |
marcozecchini | 0:9fca2b23d0ba | 232 | |
marcozecchini | 0:9fca2b23d0ba | 233 | if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &ku, 7 ) ) != 4 ) |
marcozecchini | 0:9fca2b23d0ba | 234 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 235 | |
marcozecchini | 0:9fca2b23d0ba | 236 | ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_KEY_USAGE, |
marcozecchini | 0:9fca2b23d0ba | 237 | MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ), |
marcozecchini | 0:9fca2b23d0ba | 238 | 1, buf, 4 ); |
marcozecchini | 0:9fca2b23d0ba | 239 | if( ret != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 240 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 241 | |
marcozecchini | 0:9fca2b23d0ba | 242 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 243 | } |
marcozecchini | 0:9fca2b23d0ba | 244 | |
marcozecchini | 0:9fca2b23d0ba | 245 | int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx, |
marcozecchini | 0:9fca2b23d0ba | 246 | unsigned char ns_cert_type ) |
marcozecchini | 0:9fca2b23d0ba | 247 | { |
marcozecchini | 0:9fca2b23d0ba | 248 | unsigned char buf[4]; |
marcozecchini | 0:9fca2b23d0ba | 249 | unsigned char *c; |
marcozecchini | 0:9fca2b23d0ba | 250 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 251 | |
marcozecchini | 0:9fca2b23d0ba | 252 | c = buf + 4; |
marcozecchini | 0:9fca2b23d0ba | 253 | |
marcozecchini | 0:9fca2b23d0ba | 254 | if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 ) |
marcozecchini | 0:9fca2b23d0ba | 255 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 256 | |
marcozecchini | 0:9fca2b23d0ba | 257 | ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE, |
marcozecchini | 0:9fca2b23d0ba | 258 | MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ), |
marcozecchini | 0:9fca2b23d0ba | 259 | 0, buf, 4 ); |
marcozecchini | 0:9fca2b23d0ba | 260 | if( ret != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 261 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 262 | |
marcozecchini | 0:9fca2b23d0ba | 263 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 264 | } |
marcozecchini | 0:9fca2b23d0ba | 265 | |
marcozecchini | 0:9fca2b23d0ba | 266 | static int x509_write_time( unsigned char **p, unsigned char *start, |
marcozecchini | 0:9fca2b23d0ba | 267 | const char *t, size_t size ) |
marcozecchini | 0:9fca2b23d0ba | 268 | { |
marcozecchini | 0:9fca2b23d0ba | 269 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 270 | size_t len = 0; |
marcozecchini | 0:9fca2b23d0ba | 271 | |
marcozecchini | 0:9fca2b23d0ba | 272 | /* |
marcozecchini | 0:9fca2b23d0ba | 273 | * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter) |
marcozecchini | 0:9fca2b23d0ba | 274 | */ |
marcozecchini | 0:9fca2b23d0ba | 275 | if( t[0] == '2' && t[1] == '0' && t[2] < '5' ) |
marcozecchini | 0:9fca2b23d0ba | 276 | { |
marcozecchini | 0:9fca2b23d0ba | 277 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, |
marcozecchini | 0:9fca2b23d0ba | 278 | (const unsigned char *) t + 2, |
marcozecchini | 0:9fca2b23d0ba | 279 | size - 2 ) ); |
marcozecchini | 0:9fca2b23d0ba | 280 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
marcozecchini | 0:9fca2b23d0ba | 281 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_UTC_TIME ) ); |
marcozecchini | 0:9fca2b23d0ba | 282 | } |
marcozecchini | 0:9fca2b23d0ba | 283 | else |
marcozecchini | 0:9fca2b23d0ba | 284 | { |
marcozecchini | 0:9fca2b23d0ba | 285 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, |
marcozecchini | 0:9fca2b23d0ba | 286 | (const unsigned char *) t, |
marcozecchini | 0:9fca2b23d0ba | 287 | size ) ); |
marcozecchini | 0:9fca2b23d0ba | 288 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
marcozecchini | 0:9fca2b23d0ba | 289 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_GENERALIZED_TIME ) ); |
marcozecchini | 0:9fca2b23d0ba | 290 | } |
marcozecchini | 0:9fca2b23d0ba | 291 | |
marcozecchini | 0:9fca2b23d0ba | 292 | return( (int) len ); |
marcozecchini | 0:9fca2b23d0ba | 293 | } |
marcozecchini | 0:9fca2b23d0ba | 294 | |
marcozecchini | 0:9fca2b23d0ba | 295 | int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size, |
marcozecchini | 0:9fca2b23d0ba | 296 | int (*f_rng)(void *, unsigned char *, size_t), |
marcozecchini | 0:9fca2b23d0ba | 297 | void *p_rng ) |
marcozecchini | 0:9fca2b23d0ba | 298 | { |
marcozecchini | 0:9fca2b23d0ba | 299 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 300 | const char *sig_oid; |
marcozecchini | 0:9fca2b23d0ba | 301 | size_t sig_oid_len = 0; |
marcozecchini | 0:9fca2b23d0ba | 302 | unsigned char *c, *c2; |
marcozecchini | 0:9fca2b23d0ba | 303 | unsigned char hash[64]; |
marcozecchini | 0:9fca2b23d0ba | 304 | unsigned char sig[MBEDTLS_MPI_MAX_SIZE]; |
marcozecchini | 0:9fca2b23d0ba | 305 | unsigned char tmp_buf[2048]; |
marcozecchini | 0:9fca2b23d0ba | 306 | size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len; |
marcozecchini | 0:9fca2b23d0ba | 307 | size_t len = 0; |
marcozecchini | 0:9fca2b23d0ba | 308 | mbedtls_pk_type_t pk_alg; |
marcozecchini | 0:9fca2b23d0ba | 309 | |
marcozecchini | 0:9fca2b23d0ba | 310 | /* |
marcozecchini | 0:9fca2b23d0ba | 311 | * Prepare data to be signed in tmp_buf |
marcozecchini | 0:9fca2b23d0ba | 312 | */ |
marcozecchini | 0:9fca2b23d0ba | 313 | c = tmp_buf + sizeof( tmp_buf ); |
marcozecchini | 0:9fca2b23d0ba | 314 | |
marcozecchini | 0:9fca2b23d0ba | 315 | /* Signature algorithm needed in TBS, and later for actual signature */ |
marcozecchini | 0:9fca2b23d0ba | 316 | pk_alg = mbedtls_pk_get_type( ctx->issuer_key ); |
marcozecchini | 0:9fca2b23d0ba | 317 | if( pk_alg == MBEDTLS_PK_ECKEY ) |
marcozecchini | 0:9fca2b23d0ba | 318 | pk_alg = MBEDTLS_PK_ECDSA; |
marcozecchini | 0:9fca2b23d0ba | 319 | |
marcozecchini | 0:9fca2b23d0ba | 320 | if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg, |
marcozecchini | 0:9fca2b23d0ba | 321 | &sig_oid, &sig_oid_len ) ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 322 | { |
marcozecchini | 0:9fca2b23d0ba | 323 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 324 | } |
marcozecchini | 0:9fca2b23d0ba | 325 | |
marcozecchini | 0:9fca2b23d0ba | 326 | /* |
marcozecchini | 0:9fca2b23d0ba | 327 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
marcozecchini | 0:9fca2b23d0ba | 328 | */ |
marcozecchini | 0:9fca2b23d0ba | 329 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, tmp_buf, ctx->extensions ) ); |
marcozecchini | 0:9fca2b23d0ba | 330 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) ); |
marcozecchini | 0:9fca2b23d0ba | 331 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED | |
marcozecchini | 0:9fca2b23d0ba | 332 | MBEDTLS_ASN1_SEQUENCE ) ); |
marcozecchini | 0:9fca2b23d0ba | 333 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) ); |
marcozecchini | 0:9fca2b23d0ba | 334 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
marcozecchini | 0:9fca2b23d0ba | 335 | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ); |
marcozecchini | 0:9fca2b23d0ba | 336 | |
marcozecchini | 0:9fca2b23d0ba | 337 | /* |
marcozecchini | 0:9fca2b23d0ba | 338 | * SubjectPublicKeyInfo |
marcozecchini | 0:9fca2b23d0ba | 339 | */ |
marcozecchini | 0:9fca2b23d0ba | 340 | MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->subject_key, |
marcozecchini | 0:9fca2b23d0ba | 341 | tmp_buf, c - tmp_buf ) ); |
marcozecchini | 0:9fca2b23d0ba | 342 | c -= pub_len; |
marcozecchini | 0:9fca2b23d0ba | 343 | len += pub_len; |
marcozecchini | 0:9fca2b23d0ba | 344 | |
marcozecchini | 0:9fca2b23d0ba | 345 | /* |
marcozecchini | 0:9fca2b23d0ba | 346 | * Subject ::= Name |
marcozecchini | 0:9fca2b23d0ba | 347 | */ |
marcozecchini | 0:9fca2b23d0ba | 348 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->subject ) ); |
marcozecchini | 0:9fca2b23d0ba | 349 | |
marcozecchini | 0:9fca2b23d0ba | 350 | /* |
marcozecchini | 0:9fca2b23d0ba | 351 | * Validity ::= SEQUENCE { |
marcozecchini | 0:9fca2b23d0ba | 352 | * notBefore Time, |
marcozecchini | 0:9fca2b23d0ba | 353 | * notAfter Time } |
marcozecchini | 0:9fca2b23d0ba | 354 | */ |
marcozecchini | 0:9fca2b23d0ba | 355 | sub_len = 0; |
marcozecchini | 0:9fca2b23d0ba | 356 | |
marcozecchini | 0:9fca2b23d0ba | 357 | MBEDTLS_ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_after, |
marcozecchini | 0:9fca2b23d0ba | 358 | MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) ); |
marcozecchini | 0:9fca2b23d0ba | 359 | |
marcozecchini | 0:9fca2b23d0ba | 360 | MBEDTLS_ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_before, |
marcozecchini | 0:9fca2b23d0ba | 361 | MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) ); |
marcozecchini | 0:9fca2b23d0ba | 362 | |
marcozecchini | 0:9fca2b23d0ba | 363 | len += sub_len; |
marcozecchini | 0:9fca2b23d0ba | 364 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, sub_len ) ); |
marcozecchini | 0:9fca2b23d0ba | 365 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED | |
marcozecchini | 0:9fca2b23d0ba | 366 | MBEDTLS_ASN1_SEQUENCE ) ); |
marcozecchini | 0:9fca2b23d0ba | 367 | |
marcozecchini | 0:9fca2b23d0ba | 368 | /* |
marcozecchini | 0:9fca2b23d0ba | 369 | * Issuer ::= Name |
marcozecchini | 0:9fca2b23d0ba | 370 | */ |
marcozecchini | 0:9fca2b23d0ba | 371 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->issuer ) ); |
marcozecchini | 0:9fca2b23d0ba | 372 | |
marcozecchini | 0:9fca2b23d0ba | 373 | /* |
marcozecchini | 0:9fca2b23d0ba | 374 | * Signature ::= AlgorithmIdentifier |
marcozecchini | 0:9fca2b23d0ba | 375 | */ |
marcozecchini | 0:9fca2b23d0ba | 376 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, tmp_buf, |
marcozecchini | 0:9fca2b23d0ba | 377 | sig_oid, strlen( sig_oid ), 0 ) ); |
marcozecchini | 0:9fca2b23d0ba | 378 | |
marcozecchini | 0:9fca2b23d0ba | 379 | /* |
marcozecchini | 0:9fca2b23d0ba | 380 | * Serial ::= INTEGER |
marcozecchini | 0:9fca2b23d0ba | 381 | */ |
marcozecchini | 0:9fca2b23d0ba | 382 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, tmp_buf, &ctx->serial ) ); |
marcozecchini | 0:9fca2b23d0ba | 383 | |
marcozecchini | 0:9fca2b23d0ba | 384 | /* |
marcozecchini | 0:9fca2b23d0ba | 385 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
marcozecchini | 0:9fca2b23d0ba | 386 | */ |
marcozecchini | 0:9fca2b23d0ba | 387 | sub_len = 0; |
marcozecchini | 0:9fca2b23d0ba | 388 | MBEDTLS_ASN1_CHK_ADD( sub_len, mbedtls_asn1_write_int( &c, tmp_buf, ctx->version ) ); |
marcozecchini | 0:9fca2b23d0ba | 389 | len += sub_len; |
marcozecchini | 0:9fca2b23d0ba | 390 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, sub_len ) ); |
marcozecchini | 0:9fca2b23d0ba | 391 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
marcozecchini | 0:9fca2b23d0ba | 392 | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ); |
marcozecchini | 0:9fca2b23d0ba | 393 | |
marcozecchini | 0:9fca2b23d0ba | 394 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) ); |
marcozecchini | 0:9fca2b23d0ba | 395 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED | |
marcozecchini | 0:9fca2b23d0ba | 396 | MBEDTLS_ASN1_SEQUENCE ) ); |
marcozecchini | 0:9fca2b23d0ba | 397 | |
marcozecchini | 0:9fca2b23d0ba | 398 | /* |
marcozecchini | 0:9fca2b23d0ba | 399 | * Make signature |
marcozecchini | 0:9fca2b23d0ba | 400 | */ |
marcozecchini | 0:9fca2b23d0ba | 401 | mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash ); |
marcozecchini | 0:9fca2b23d0ba | 402 | |
marcozecchini | 0:9fca2b23d0ba | 403 | if( ( ret = mbedtls_pk_sign( ctx->issuer_key, ctx->md_alg, hash, 0, sig, &sig_len, |
marcozecchini | 0:9fca2b23d0ba | 404 | f_rng, p_rng ) ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 405 | { |
marcozecchini | 0:9fca2b23d0ba | 406 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 407 | } |
marcozecchini | 0:9fca2b23d0ba | 408 | |
marcozecchini | 0:9fca2b23d0ba | 409 | /* |
marcozecchini | 0:9fca2b23d0ba | 410 | * Write data to output buffer |
marcozecchini | 0:9fca2b23d0ba | 411 | */ |
marcozecchini | 0:9fca2b23d0ba | 412 | c2 = buf + size; |
marcozecchini | 0:9fca2b23d0ba | 413 | MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, buf, |
marcozecchini | 0:9fca2b23d0ba | 414 | sig_oid, sig_oid_len, sig, sig_len ) ); |
marcozecchini | 0:9fca2b23d0ba | 415 | |
marcozecchini | 0:9fca2b23d0ba | 416 | if( len > (size_t)( c2 - buf ) ) |
marcozecchini | 0:9fca2b23d0ba | 417 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
marcozecchini | 0:9fca2b23d0ba | 418 | |
marcozecchini | 0:9fca2b23d0ba | 419 | c2 -= len; |
marcozecchini | 0:9fca2b23d0ba | 420 | memcpy( c2, c, len ); |
marcozecchini | 0:9fca2b23d0ba | 421 | |
marcozecchini | 0:9fca2b23d0ba | 422 | len += sig_and_oid_len; |
marcozecchini | 0:9fca2b23d0ba | 423 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) ); |
marcozecchini | 0:9fca2b23d0ba | 424 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, MBEDTLS_ASN1_CONSTRUCTED | |
marcozecchini | 0:9fca2b23d0ba | 425 | MBEDTLS_ASN1_SEQUENCE ) ); |
marcozecchini | 0:9fca2b23d0ba | 426 | |
marcozecchini | 0:9fca2b23d0ba | 427 | return( (int) len ); |
marcozecchini | 0:9fca2b23d0ba | 428 | } |
marcozecchini | 0:9fca2b23d0ba | 429 | |
marcozecchini | 0:9fca2b23d0ba | 430 | #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n" |
marcozecchini | 0:9fca2b23d0ba | 431 | #define PEM_END_CRT "-----END CERTIFICATE-----\n" |
marcozecchini | 0:9fca2b23d0ba | 432 | |
marcozecchini | 0:9fca2b23d0ba | 433 | #if defined(MBEDTLS_PEM_WRITE_C) |
marcozecchini | 0:9fca2b23d0ba | 434 | int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *crt, unsigned char *buf, size_t size, |
marcozecchini | 0:9fca2b23d0ba | 435 | int (*f_rng)(void *, unsigned char *, size_t), |
marcozecchini | 0:9fca2b23d0ba | 436 | void *p_rng ) |
marcozecchini | 0:9fca2b23d0ba | 437 | { |
marcozecchini | 0:9fca2b23d0ba | 438 | int ret; |
marcozecchini | 0:9fca2b23d0ba | 439 | unsigned char output_buf[4096]; |
marcozecchini | 0:9fca2b23d0ba | 440 | size_t olen = 0; |
marcozecchini | 0:9fca2b23d0ba | 441 | |
marcozecchini | 0:9fca2b23d0ba | 442 | if( ( ret = mbedtls_x509write_crt_der( crt, output_buf, sizeof(output_buf), |
marcozecchini | 0:9fca2b23d0ba | 443 | f_rng, p_rng ) ) < 0 ) |
marcozecchini | 0:9fca2b23d0ba | 444 | { |
marcozecchini | 0:9fca2b23d0ba | 445 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 446 | } |
marcozecchini | 0:9fca2b23d0ba | 447 | |
marcozecchini | 0:9fca2b23d0ba | 448 | if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT, |
marcozecchini | 0:9fca2b23d0ba | 449 | output_buf + sizeof(output_buf) - ret, |
marcozecchini | 0:9fca2b23d0ba | 450 | ret, buf, size, &olen ) ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 451 | { |
marcozecchini | 0:9fca2b23d0ba | 452 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 453 | } |
marcozecchini | 0:9fca2b23d0ba | 454 | |
marcozecchini | 0:9fca2b23d0ba | 455 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 456 | } |
marcozecchini | 0:9fca2b23d0ba | 457 | #endif /* MBEDTLS_PEM_WRITE_C */ |
marcozecchini | 0:9fca2b23d0ba | 458 | |
marcozecchini | 0:9fca2b23d0ba | 459 | #endif /* MBEDTLS_X509_CRT_WRITE_C */ |