Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * X.509 Certificate Signing Request writing |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
| 45 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 46 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 47 | */ |
| 48 | /* |
| 49 | * References: |
| 50 | * - CSRs: PKCS#10 v1.7 aka RFC 2986 |
| 51 | * - attributes: PKCS#9 v2.0 aka RFC 2985 |
| 52 | */ |
| 53 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 55 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 56 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 58 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 59 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | #if defined(MBEDTLS_X509_CSR_WRITE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 61 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 62 | #include "mbedtls/x509_csr.h" |
| 63 | #include "mbedtls/oid.h" |
| 64 | #include "mbedtls/asn1write.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 65 | #include "mbedtls/platform_util.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 66 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 67 | #include <string.h> |
| 68 | #include <stdlib.h> |
| 69 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | #if defined(MBEDTLS_PEM_WRITE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 71 | #include "mbedtls/pem.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 72 | #endif |
| 73 | |
k-stachowiak | e79c939 | 2019-05-31 20:11:26 +0200 | [diff] [blame] | 74 | /* |
| 75 | * For the currently used signature algorithms the buffer to store any signature |
| 76 | * must be at least of size MAX(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE) |
| 77 | */ |
| 78 | #if MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_MPI_MAX_SIZE |
| 79 | #define SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN |
| 80 | #else |
| 81 | #define SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE |
| 82 | #endif |
| 83 | |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 84 | #if defined(MBEDTLS_PLATFORM_C) |
| 85 | #include "mbedtls/platform.h" |
| 86 | #else |
| 87 | #include <stdlib.h> |
| 88 | #define mbedtls_calloc calloc |
| 89 | #define mbedtls_free free |
| 90 | #endif |
| 91 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 93 | { |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 94 | memset( ctx, 0, sizeof( mbedtls_x509write_csr ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 98 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | mbedtls_asn1_free_named_data_list( &ctx->subject ); |
| 100 | mbedtls_asn1_free_named_data_list( &ctx->extensions ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 101 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 102 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x509write_csr ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 106 | { |
| 107 | ctx->md_alg = md_alg; |
| 108 | } |
| 109 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 110 | void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 111 | { |
| 112 | ctx->key = key; |
| 113 | } |
| 114 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx, |
Paul Bakker | 50dc850 | 2013-10-28 21:19:10 +0100 | [diff] [blame] | 116 | const char *subject_name ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 117 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | return mbedtls_x509_string_to_names( &ctx->subject, subject_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 122 | const char *oid, size_t oid_len, |
| 123 | const unsigned char *val, size_t val_len ) |
| 124 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 125 | return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 126 | 0, val, val_len ); |
| 127 | } |
| 128 | |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 129 | static size_t csr_get_unused_bits_for_named_bitstring( unsigned char bitstring, |
| 130 | size_t bit_offset ) |
| 131 | { |
| 132 | size_t unused_bits; |
| 133 | |
| 134 | /* Count the unused bits removing trailing 0s */ |
| 135 | for( unused_bits = bit_offset; unused_bits < 8; unused_bits++ ) |
| 136 | if( ( ( bitstring >> unused_bits ) & 0x1 ) != 0 ) |
| 137 | break; |
| 138 | |
| 139 | return( unused_bits ); |
| 140 | } |
| 141 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 143 | { |
| 144 | unsigned char buf[4]; |
| 145 | unsigned char *c; |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 146 | size_t unused_bits; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 147 | int ret; |
| 148 | |
| 149 | c = buf + 4; |
| 150 | |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 151 | unused_bits = csr_get_unused_bits_for_named_bitstring( key_usage, 0 ); |
| 152 | ret = mbedtls_asn1_write_bitstring( &c, buf, &key_usage, 8 - unused_bits ); |
| 153 | |
| 154 | if( ret < 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 155 | return( ret ); |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 156 | else if( ret < 3 || ret > 4 ) |
| 157 | return( MBEDTLS_ERR_X509_INVALID_FORMAT ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 158 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_KEY_USAGE, |
| 160 | MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ), |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 161 | c, (size_t)ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 162 | if( ret != 0 ) |
| 163 | return( ret ); |
| 164 | |
| 165 | return( 0 ); |
| 166 | } |
| 167 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 169 | unsigned char ns_cert_type ) |
| 170 | { |
| 171 | unsigned char buf[4]; |
| 172 | unsigned char *c; |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 173 | size_t unused_bits; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 174 | int ret; |
| 175 | |
| 176 | c = buf + 4; |
| 177 | |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 178 | unused_bits = csr_get_unused_bits_for_named_bitstring( ns_cert_type, 0 ); |
| 179 | ret = mbedtls_asn1_write_bitstring( &c, |
| 180 | buf, |
| 181 | &ns_cert_type, |
| 182 | 8 - unused_bits ); |
| 183 | |
| 184 | if( ret < 0 ) |
| 185 | return( ret ); |
| 186 | else if( ret < 3 || ret > 4 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 187 | return( ret ); |
| 188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE, |
| 190 | MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ), |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 191 | c, (size_t)ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 192 | if( ret != 0 ) |
| 193 | return( ret ); |
| 194 | |
| 195 | return( 0 ); |
| 196 | } |
| 197 | |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 198 | static int x509write_csr_der_internal( mbedtls_x509write_csr *ctx, |
| 199 | unsigned char *buf, |
| 200 | size_t size, unsigned char *sig, |
| 201 | int (*f_rng)(void *, unsigned char *, size_t), |
| 202 | void *p_rng ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 203 | { |
| 204 | int ret; |
| 205 | const char *sig_oid; |
| 206 | size_t sig_oid_len = 0; |
| 207 | unsigned char *c, *c2; |
| 208 | unsigned char hash[64]; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 209 | size_t pub_len = 0, sig_and_oid_len = 0, sig_len; |
| 210 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | mbedtls_pk_type_t pk_alg; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 212 | |
| 213 | /* |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 214 | * Writing strategy: |
| 215 | * 1. start writing from the back of buf |
| 216 | * 2. sign the written data and place the signature at the start of buf |
| 217 | * 3. compact memory locations by moving the signature towards right |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 218 | */ |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 219 | c = buf + size; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 220 | |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 221 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, buf, |
| 222 | ctx->extensions ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 223 | |
| 224 | if( len ) |
| 225 | { |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 226 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
| 227 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, |
| 228 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 229 | |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 230 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
| 231 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, |
| 232 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 233 | |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 234 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( &c, buf, |
| 235 | MBEDTLS_OID_PKCS9_CSR_EXT_REQ, |
| 236 | MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_CSR_EXT_REQ ) ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 237 | |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 238 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
| 239 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, |
| 240 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 243 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
| 244 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, |
| 245 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 246 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 247 | MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->key, |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 248 | buf, c - buf ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 249 | c -= pub_len; |
| 250 | len += pub_len; |
| 251 | |
| 252 | /* |
| 253 | * Subject ::= Name |
| 254 | */ |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 255 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, buf, |
| 256 | ctx->subject ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 257 | |
| 258 | /* |
| 259 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 260 | */ |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 261 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 262 | |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 263 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
| 264 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, |
| 265 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | * Prepare signature |
| 269 | */ |
Gilles Peskine | 3a3b161 | 2020-01-21 16:56:03 +0100 | [diff] [blame] | 270 | ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash ); |
| 271 | if( ret != 0 ) |
| 272 | return( ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 273 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len, |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 275 | f_rng, p_rng ) ) != 0 ) |
| 276 | { |
| 277 | return( ret ); |
| 278 | } |
| 279 | |
| 280 | if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_RSA ) ) |
| 281 | pk_alg = MBEDTLS_PK_RSA; |
| 282 | else if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_ECDSA ) ) |
| 283 | pk_alg = MBEDTLS_PK_ECDSA; |
| 284 | else |
Hanno Becker | d8a6f7c | 2017-09-22 16:05:43 +0100 | [diff] [blame] | 285 | return( MBEDTLS_ERR_X509_INVALID_ALG ); |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 286 | |
| 287 | if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg, |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 288 | &sig_oid, &sig_oid_len ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 289 | { |
| 290 | return( ret ); |
| 291 | } |
| 292 | |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 293 | /* reserve space for the signature at the end of buf */ |
| 294 | memmove( buf, c, len ); |
| 295 | |
| 296 | /* copy the signature */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 297 | c2 = buf + size; |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 298 | MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, |
| 299 | buf + len, sig_oid, sig_oid_len, sig, sig_len ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 300 | |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 301 | /* compact oids and signature memory locations */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 302 | c2 -= len; |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 303 | memmove( c2, buf, len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 304 | |
| 305 | len += sig_and_oid_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 306 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) ); |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 307 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, |
| 308 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ); |
| 309 | memset( buf, 0, c2 - buf); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 310 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 311 | return( (int) len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Doru Gucea | afc2717 | 2018-12-14 21:08:35 +0200 | [diff] [blame^] | 314 | int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, |
| 315 | size_t size, |
| 316 | int (*f_rng)(void *, unsigned char *, size_t), |
| 317 | void *p_rng ) |
| 318 | { |
| 319 | int ret; |
| 320 | unsigned char *sig; |
| 321 | |
| 322 | if( ( sig = mbedtls_calloc( 1, MBEDTLS_MPI_MAX_SIZE ) ) == NULL ) |
| 323 | { |
| 324 | return( MBEDTLS_ERR_ASN1_ALLOC_FAILED ); |
| 325 | } |
| 326 | |
| 327 | ret = x509write_csr_der_internal( ctx, buf, size, sig, f_rng, p_rng ); |
| 328 | |
| 329 | mbedtls_free( sig ); |
| 330 | |
| 331 | return( ret ); |
| 332 | } |
| 333 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 334 | #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n" |
| 335 | #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n" |
| 336 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 337 | #if defined(MBEDTLS_PEM_WRITE_C) |
| 338 | int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 339 | int (*f_rng)(void *, unsigned char *, size_t), |
| 340 | void *p_rng ) |
| 341 | { |
| 342 | int ret; |
| 343 | unsigned char output_buf[4096]; |
| 344 | size_t olen = 0; |
| 345 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 346 | if( ( ret = mbedtls_x509write_csr_der( ctx, output_buf, sizeof(output_buf), |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 347 | f_rng, p_rng ) ) < 0 ) |
| 348 | { |
| 349 | return( ret ); |
| 350 | } |
| 351 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 352 | if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 353 | output_buf + sizeof(output_buf) - ret, |
| 354 | ret, buf, size, &olen ) ) != 0 ) |
| 355 | { |
| 356 | return( ret ); |
| 357 | } |
| 358 | |
| 359 | return( 0 ); |
| 360 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 361 | #endif /* MBEDTLS_PEM_WRITE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 362 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 363 | #endif /* MBEDTLS_X509_CSR_WRITE_C */ |