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