mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /*
ansond 0:137634ff4186 2 * X.509 Certificate Signing Request writing
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 9 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 10 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 11 * (at your option) any later version.
ansond 0:137634ff4186 12 *
ansond 0:137634ff4186 13 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 16 * GNU General Public License for more details.
ansond 0:137634ff4186 17 *
ansond 0:137634ff4186 18 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 19 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 21 */
ansond 0:137634ff4186 22 /*
ansond 0:137634ff4186 23 * References:
ansond 0:137634ff4186 24 * - CSRs: PKCS#10 v1.7 aka RFC 2986
ansond 0:137634ff4186 25 * - attributes: PKCS#9 v2.0 aka RFC 2985
ansond 0:137634ff4186 26 */
ansond 0:137634ff4186 27
ansond 0:137634ff4186 28 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 29 #include "polarssl/config.h"
ansond 0:137634ff4186 30 #else
ansond 0:137634ff4186 31 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 32 #endif
ansond 0:137634ff4186 33
ansond 0:137634ff4186 34 #if defined(POLARSSL_X509_CSR_WRITE_C)
ansond 0:137634ff4186 35
ansond 0:137634ff4186 36 #include "polarssl/x509_csr.h"
ansond 0:137634ff4186 37 #include "polarssl/oid.h"
ansond 0:137634ff4186 38 #include "polarssl/asn1write.h"
ansond 0:137634ff4186 39
ansond 0:137634ff4186 40 #include <string.h>
ansond 0:137634ff4186 41 #include <stdlib.h>
ansond 0:137634ff4186 42
ansond 0:137634ff4186 43 #if defined(POLARSSL_PEM_WRITE_C)
ansond 0:137634ff4186 44 #include "polarssl/pem.h"
ansond 0:137634ff4186 45 #endif
ansond 0:137634ff4186 46
ansond 0:137634ff4186 47 /* Implementation that should never be optimized out by the compiler */
ansond 0:137634ff4186 48 static void polarssl_zeroize( void *v, size_t n ) {
ansond 0:137634ff4186 49 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
ansond 0:137634ff4186 50 }
ansond 0:137634ff4186 51
ansond 0:137634ff4186 52 void x509write_csr_init( x509write_csr *ctx )
ansond 0:137634ff4186 53 {
ansond 0:137634ff4186 54 memset( ctx, 0, sizeof(x509write_csr) );
ansond 0:137634ff4186 55 }
ansond 0:137634ff4186 56
ansond 0:137634ff4186 57 void x509write_csr_free( x509write_csr *ctx )
ansond 0:137634ff4186 58 {
ansond 0:137634ff4186 59 asn1_free_named_data_list( &ctx->subject );
ansond 0:137634ff4186 60 asn1_free_named_data_list( &ctx->extensions );
ansond 0:137634ff4186 61
ansond 0:137634ff4186 62 polarssl_zeroize( ctx, sizeof(x509write_csr) );
ansond 0:137634ff4186 63 }
ansond 0:137634ff4186 64
ansond 0:137634ff4186 65 void x509write_csr_set_md_alg( x509write_csr *ctx, md_type_t md_alg )
ansond 0:137634ff4186 66 {
ansond 0:137634ff4186 67 ctx->md_alg = md_alg;
ansond 0:137634ff4186 68 }
ansond 0:137634ff4186 69
ansond 0:137634ff4186 70 void x509write_csr_set_key( x509write_csr *ctx, pk_context *key )
ansond 0:137634ff4186 71 {
ansond 0:137634ff4186 72 ctx->key = key;
ansond 0:137634ff4186 73 }
ansond 0:137634ff4186 74
ansond 0:137634ff4186 75 int x509write_csr_set_subject_name( x509write_csr *ctx,
ansond 0:137634ff4186 76 const char *subject_name )
ansond 0:137634ff4186 77 {
ansond 0:137634ff4186 78 return x509_string_to_names( &ctx->subject, subject_name );
ansond 0:137634ff4186 79 }
ansond 0:137634ff4186 80
ansond 0:137634ff4186 81 int x509write_csr_set_extension( x509write_csr *ctx,
ansond 0:137634ff4186 82 const char *oid, size_t oid_len,
ansond 0:137634ff4186 83 const unsigned char *val, size_t val_len )
ansond 0:137634ff4186 84 {
ansond 0:137634ff4186 85 return x509_set_extension( &ctx->extensions, oid, oid_len,
ansond 0:137634ff4186 86 0, val, val_len );
ansond 0:137634ff4186 87 }
ansond 0:137634ff4186 88
ansond 0:137634ff4186 89 int x509write_csr_set_key_usage( x509write_csr *ctx, unsigned char key_usage )
ansond 0:137634ff4186 90 {
ansond 0:137634ff4186 91 unsigned char buf[4];
ansond 0:137634ff4186 92 unsigned char *c;
ansond 0:137634ff4186 93 int ret;
ansond 0:137634ff4186 94
ansond 0:137634ff4186 95 c = buf + 4;
ansond 0:137634ff4186 96
ansond 0:137634ff4186 97 if( ( ret = asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
ansond 0:137634ff4186 98 return( ret );
ansond 0:137634ff4186 99
ansond 0:137634ff4186 100 ret = x509write_csr_set_extension( ctx, OID_KEY_USAGE,
ansond 0:137634ff4186 101 OID_SIZE( OID_KEY_USAGE ),
ansond 0:137634ff4186 102 buf, 4 );
ansond 0:137634ff4186 103 if( ret != 0 )
ansond 0:137634ff4186 104 return( ret );
ansond 0:137634ff4186 105
ansond 0:137634ff4186 106 return( 0 );
ansond 0:137634ff4186 107 }
ansond 0:137634ff4186 108
ansond 0:137634ff4186 109 int x509write_csr_set_ns_cert_type( x509write_csr *ctx,
ansond 0:137634ff4186 110 unsigned char ns_cert_type )
ansond 0:137634ff4186 111 {
ansond 0:137634ff4186 112 unsigned char buf[4];
ansond 0:137634ff4186 113 unsigned char *c;
ansond 0:137634ff4186 114 int ret;
ansond 0:137634ff4186 115
ansond 0:137634ff4186 116 c = buf + 4;
ansond 0:137634ff4186 117
ansond 0:137634ff4186 118 if( ( ret = asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
ansond 0:137634ff4186 119 return( ret );
ansond 0:137634ff4186 120
ansond 0:137634ff4186 121 ret = x509write_csr_set_extension( ctx, OID_NS_CERT_TYPE,
ansond 0:137634ff4186 122 OID_SIZE( OID_NS_CERT_TYPE ),
ansond 0:137634ff4186 123 buf, 4 );
ansond 0:137634ff4186 124 if( ret != 0 )
ansond 0:137634ff4186 125 return( ret );
ansond 0:137634ff4186 126
ansond 0:137634ff4186 127 return( 0 );
ansond 0:137634ff4186 128 }
ansond 0:137634ff4186 129
ansond 0:137634ff4186 130 int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size,
ansond 0:137634ff4186 131 int (*f_rng)(void *, unsigned char *, size_t),
ansond 0:137634ff4186 132 void *p_rng )
ansond 0:137634ff4186 133 {
ansond 0:137634ff4186 134 int ret;
ansond 0:137634ff4186 135 const char *sig_oid;
ansond 0:137634ff4186 136 size_t sig_oid_len = 0;
ansond 0:137634ff4186 137 unsigned char *c, *c2;
ansond 0:137634ff4186 138 unsigned char hash[64];
ansond 0:137634ff4186 139 unsigned char sig[POLARSSL_MPI_MAX_SIZE];
ansond 0:137634ff4186 140 unsigned char tmp_buf[2048];
ansond 0:137634ff4186 141 size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
ansond 0:137634ff4186 142 size_t len = 0;
ansond 0:137634ff4186 143 pk_type_t pk_alg;
ansond 0:137634ff4186 144
ansond 0:137634ff4186 145 /*
ansond 0:137634ff4186 146 * Prepare data to be signed in tmp_buf
ansond 0:137634ff4186 147 */
ansond 0:137634ff4186 148 c = tmp_buf + sizeof( tmp_buf );
ansond 0:137634ff4186 149
ansond 0:137634ff4186 150 ASN1_CHK_ADD( len, x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
ansond 0:137634ff4186 151
ansond 0:137634ff4186 152 if( len )
ansond 0:137634ff4186 153 {
ansond 0:137634ff4186 154 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
ansond 0:137634ff4186 155 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
ansond 0:137634ff4186 156 ASN1_SEQUENCE ) );
ansond 0:137634ff4186 157
ansond 0:137634ff4186 158 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
ansond 0:137634ff4186 159 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
ansond 0:137634ff4186 160 ASN1_SET ) );
ansond 0:137634ff4186 161
ansond 0:137634ff4186 162 ASN1_CHK_ADD( len, asn1_write_oid( &c, tmp_buf, OID_PKCS9_CSR_EXT_REQ,
ansond 0:137634ff4186 163 OID_SIZE( OID_PKCS9_CSR_EXT_REQ ) ) );
ansond 0:137634ff4186 164
ansond 0:137634ff4186 165 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
ansond 0:137634ff4186 166 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
ansond 0:137634ff4186 167 ASN1_SEQUENCE ) );
ansond 0:137634ff4186 168 }
ansond 0:137634ff4186 169
ansond 0:137634ff4186 170 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
ansond 0:137634ff4186 171 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
ansond 0:137634ff4186 172 ASN1_CONTEXT_SPECIFIC ) );
ansond 0:137634ff4186 173
ansond 0:137634ff4186 174 ASN1_CHK_ADD( pub_len, pk_write_pubkey_der( ctx->key,
ansond 0:137634ff4186 175 tmp_buf, c - tmp_buf ) );
ansond 0:137634ff4186 176 c -= pub_len;
ansond 0:137634ff4186 177 len += pub_len;
ansond 0:137634ff4186 178
ansond 0:137634ff4186 179 /*
ansond 0:137634ff4186 180 * Subject ::= Name
ansond 0:137634ff4186 181 */
ansond 0:137634ff4186 182 ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->subject ) );
ansond 0:137634ff4186 183
ansond 0:137634ff4186 184 /*
ansond 0:137634ff4186 185 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
ansond 0:137634ff4186 186 */
ansond 0:137634ff4186 187 ASN1_CHK_ADD( len, asn1_write_int( &c, tmp_buf, 0 ) );
ansond 0:137634ff4186 188
ansond 0:137634ff4186 189 ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
ansond 0:137634ff4186 190 ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
ansond 0:137634ff4186 191 ASN1_SEQUENCE ) );
ansond 0:137634ff4186 192
ansond 0:137634ff4186 193 /*
ansond 0:137634ff4186 194 * Prepare signature
ansond 0:137634ff4186 195 */
ansond 0:137634ff4186 196 md( md_info_from_type( ctx->md_alg ), c, len, hash );
ansond 0:137634ff4186 197
ansond 0:137634ff4186 198 pk_alg = pk_get_type( ctx->key );
ansond 0:137634ff4186 199 if( pk_alg == POLARSSL_PK_ECKEY )
ansond 0:137634ff4186 200 pk_alg = POLARSSL_PK_ECDSA;
ansond 0:137634ff4186 201
ansond 0:137634ff4186 202 if( ( ret = pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
ansond 0:137634ff4186 203 f_rng, p_rng ) ) != 0 ||
ansond 0:137634ff4186 204 ( ret = oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
ansond 0:137634ff4186 205 &sig_oid, &sig_oid_len ) ) != 0 )
ansond 0:137634ff4186 206 {
ansond 0:137634ff4186 207 return( ret );
ansond 0:137634ff4186 208 }
ansond 0:137634ff4186 209
ansond 0:137634ff4186 210 /*
ansond 0:137634ff4186 211 * Write data to output buffer
ansond 0:137634ff4186 212 */
ansond 0:137634ff4186 213 c2 = buf + size;
ansond 0:137634ff4186 214 ASN1_CHK_ADD( sig_and_oid_len, x509_write_sig( &c2, buf,
ansond 0:137634ff4186 215 sig_oid, sig_oid_len, sig, sig_len ) );
ansond 0:137634ff4186 216
ansond 0:137634ff4186 217 c2 -= len;
ansond 0:137634ff4186 218 memcpy( c2, c, len );
ansond 0:137634ff4186 219
ansond 0:137634ff4186 220 len += sig_and_oid_len;
ansond 0:137634ff4186 221 ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
ansond 0:137634ff4186 222 ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED |
ansond 0:137634ff4186 223 ASN1_SEQUENCE ) );
ansond 0:137634ff4186 224
ansond 0:137634ff4186 225 return( (int) len );
ansond 0:137634ff4186 226 }
ansond 0:137634ff4186 227
ansond 0:137634ff4186 228 #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
ansond 0:137634ff4186 229 #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
ansond 0:137634ff4186 230
ansond 0:137634ff4186 231 #if defined(POLARSSL_PEM_WRITE_C)
ansond 0:137634ff4186 232 int x509write_csr_pem( x509write_csr *ctx, unsigned char *buf, size_t size,
ansond 0:137634ff4186 233 int (*f_rng)(void *, unsigned char *, size_t),
ansond 0:137634ff4186 234 void *p_rng )
ansond 0:137634ff4186 235 {
ansond 0:137634ff4186 236 int ret;
ansond 0:137634ff4186 237 unsigned char output_buf[4096];
ansond 0:137634ff4186 238 size_t olen = 0;
ansond 0:137634ff4186 239
ansond 0:137634ff4186 240 if( ( ret = x509write_csr_der( ctx, output_buf, sizeof(output_buf),
ansond 0:137634ff4186 241 f_rng, p_rng ) ) < 0 )
ansond 0:137634ff4186 242 {
ansond 0:137634ff4186 243 return( ret );
ansond 0:137634ff4186 244 }
ansond 0:137634ff4186 245
ansond 0:137634ff4186 246 if( ( ret = pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
ansond 0:137634ff4186 247 output_buf + sizeof(output_buf) - ret,
ansond 0:137634ff4186 248 ret, buf, size, &olen ) ) != 0 )
ansond 0:137634ff4186 249 {
ansond 0:137634ff4186 250 return( ret );
ansond 0:137634ff4186 251 }
ansond 0:137634ff4186 252
ansond 0:137634ff4186 253 return( 0 );
ansond 0:137634ff4186 254 }
ansond 0:137634ff4186 255 #endif /* POLARSSL_PEM_WRITE_C */
ansond 0:137634ff4186 256
ansond 0:137634ff4186 257 #endif /* POLARSSL_X509_CSR_WRITE_C */
ansond 0:137634ff4186 258