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