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