mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers x509write_crt.c Source File

x509write_crt.c

00001 /*
00002  *  X.509 certificate writing
00003  *
00004  *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
00005  *
00006  *  This file is part of mbed TLS (https://tls.mbed.org)
00007  *
00008  *  This program is free software; you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation; either version 2 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License along
00019  *  with this program; if not, write to the Free Software Foundation, Inc.,
00020  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00021  */
00022 /*
00023  * References:
00024  * - certificates: RFC 5280, updated by RFC 6818
00025  * - CSRs: PKCS#10 v1.7 aka RFC 2986
00026  * - attributes: PKCS#9 v2.0 aka RFC 2985
00027  */
00028 
00029 #if !defined(POLARSSL_CONFIG_FILE)
00030 #include "polarssl/config.h"
00031 #else
00032 #include POLARSSL_CONFIG_FILE
00033 #endif
00034 
00035 #if defined(POLARSSL_X509_CRT_WRITE_C)
00036 
00037 #include "polarssl/x509_crt.h"
00038 #include "polarssl/oid.h"
00039 #include "polarssl/asn1write.h"
00040 #include "polarssl/sha1.h"
00041 
00042 #include <string.h>
00043 
00044 #if defined(POLARSSL_PEM_WRITE_C)
00045 #include "polarssl/pem.h"
00046 #endif /* POLARSSL_PEM_WRITE_C */
00047 
00048 /* Implementation that should never be optimized out by the compiler */
00049 static void polarssl_zeroize( void *v, size_t n ) {
00050     volatile unsigned char *p = v; while( n-- ) *p++ = 0;
00051 }
00052 
00053 void x509write_crt_init( x509write_cert *ctx )
00054 {
00055     memset( ctx, 0, sizeof(x509write_cert) );
00056 
00057     mpi_init( &ctx->serial );
00058     ctx->version = X509_CRT_VERSION_3;
00059 }
00060 
00061 void x509write_crt_free( x509write_cert *ctx )
00062 {
00063     mpi_free( &ctx->serial );
00064 
00065     asn1_free_named_data_list( &ctx->subject );
00066     asn1_free_named_data_list( &ctx->issuer );
00067     asn1_free_named_data_list( &ctx->extensions );
00068 
00069     polarssl_zeroize( ctx, sizeof(x509write_cert) );
00070 }
00071 
00072 void x509write_crt_set_version( x509write_cert *ctx, int version )
00073 {
00074     ctx->version = version;
00075 }
00076 
00077 void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg )
00078 {
00079     ctx->md_alg = md_alg;
00080 }
00081 
00082 void x509write_crt_set_subject_key( x509write_cert *ctx, pk_context *key )
00083 {
00084     ctx->subject_key = key;
00085 }
00086 
00087 void x509write_crt_set_issuer_key( x509write_cert *ctx, pk_context *key )
00088 {
00089     ctx->issuer_key = key;
00090 }
00091 
00092 int x509write_crt_set_subject_name( x509write_cert *ctx,
00093                                     const char *subject_name )
00094 {
00095     return x509_string_to_names( &ctx->subject, subject_name );
00096 }
00097 
00098 int x509write_crt_set_issuer_name( x509write_cert *ctx,
00099                                    const char *issuer_name )
00100 {
00101     return x509_string_to_names( &ctx->issuer, issuer_name );
00102 }
00103 
00104 int x509write_crt_set_serial( x509write_cert *ctx, const mpi *serial )
00105 {
00106     int ret;
00107 
00108     if( ( ret = mpi_copy( &ctx->serial, serial ) ) != 0 )
00109         return( ret );
00110 
00111     return( 0 );
00112 }
00113 
00114 int x509write_crt_set_validity( x509write_cert *ctx, const char *not_before,
00115                                 const char *not_after )
00116 {
00117     if( strlen( not_before ) != X509_RFC5280_UTC_TIME_LEN - 1 ||
00118         strlen( not_after )  != X509_RFC5280_UTC_TIME_LEN - 1 )
00119     {
00120         return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
00121     }
00122     strncpy( ctx->not_before, not_before, X509_RFC5280_UTC_TIME_LEN );
00123     strncpy( ctx->not_after , not_after , X509_RFC5280_UTC_TIME_LEN );
00124     ctx->not_before[X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
00125     ctx->not_after[X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
00126 
00127     return( 0 );
00128 }
00129 
00130 int x509write_crt_set_extension( x509write_cert *ctx,
00131                                  const char *oid, size_t oid_len,
00132                                  int critical,
00133                                  const unsigned char *val, size_t val_len )
00134 {
00135     return x509_set_extension( &ctx->extensions, oid, oid_len,
00136                                critical, val, val_len );
00137 }
00138 
00139 int x509write_crt_set_basic_constraints( x509write_cert *ctx,
00140                                          int is_ca, int max_pathlen )
00141 {
00142     int ret;
00143     unsigned char buf[9];
00144     unsigned char *c = buf + sizeof(buf);
00145     size_t len = 0;
00146 
00147     memset( buf, 0, sizeof(buf) );
00148 
00149     if( is_ca && max_pathlen > 127 )
00150         return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
00151 
00152     if( is_ca )
00153     {
00154         if( max_pathlen >= 0 )
00155         {
00156             ASN1_CHK_ADD( len, asn1_write_int( &c, buf, max_pathlen ) );
00157         }
00158         ASN1_CHK_ADD( len, asn1_write_bool( &c, buf, 1 ) );
00159     }
00160 
00161     ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
00162     ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED |
00163                                                 ASN1_SEQUENCE ) );
00164 
00165     return x509write_crt_set_extension( ctx, OID_BASIC_CONSTRAINTS,
00166                                         OID_SIZE( OID_BASIC_CONSTRAINTS ),
00167                                         0, buf + sizeof(buf) - len, len );
00168 }
00169 
00170 #if defined(POLARSSL_SHA1_C)
00171 int x509write_crt_set_subject_key_identifier( x509write_cert *ctx )
00172 {
00173     int ret;
00174     unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
00175     unsigned char *c = buf + sizeof(buf);
00176     size_t len = 0;
00177 
00178     memset( buf, 0, sizeof(buf) );
00179     ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, ctx->subject_key ) );
00180 
00181     sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
00182     c = buf + sizeof(buf) - 20;
00183     len = 20;
00184 
00185     ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
00186     ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_OCTET_STRING ) );
00187 
00188     return x509write_crt_set_extension( ctx, OID_SUBJECT_KEY_IDENTIFIER,
00189                                         OID_SIZE( OID_SUBJECT_KEY_IDENTIFIER ),
00190                                         0, buf + sizeof(buf) - len, len );
00191 }
00192 
00193 int x509write_crt_set_authority_key_identifier( x509write_cert *ctx )
00194 {
00195     int ret;
00196     unsigned char buf[POLARSSL_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
00197     unsigned char *c = buf + sizeof(buf);
00198     size_t len = 0;
00199 
00200     memset( buf, 0, sizeof(buf) );
00201     ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, ctx->issuer_key ) );
00202 
00203     sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
00204     c = buf + sizeof(buf) - 20;
00205     len = 20;
00206 
00207     ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
00208     ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONTEXT_SPECIFIC | 0 ) );
00209 
00210     ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
00211     ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED |
00212                                                 ASN1_SEQUENCE ) );
00213 
00214     return x509write_crt_set_extension( ctx, OID_AUTHORITY_KEY_IDENTIFIER,
00215                                    OID_SIZE( OID_AUTHORITY_KEY_IDENTIFIER ),
00216                                    0, buf + sizeof(buf) - len, len );
00217 }
00218 #endif /* POLARSSL_SHA1_C */
00219 
00220 int x509write_crt_set_key_usage( x509write_cert *ctx, unsigned char key_usage )
00221 {
00222     unsigned char buf[4];
00223     unsigned char *c;
00224     int ret;
00225 
00226     c = buf + 4;
00227 
00228     if( ( ret = asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
00229         return( ret );
00230 
00231     ret = x509write_crt_set_extension( ctx, OID_KEY_USAGE,
00232                                        OID_SIZE( OID_KEY_USAGE ),
00233                                        1, buf, 4 );
00234     if( ret != 0 )
00235         return( ret );
00236 
00237     return( 0 );
00238 }
00239 
00240 int x509write_crt_set_ns_cert_type( x509write_cert *ctx,
00241                                     unsigned char ns_cert_type )
00242 {
00243     unsigned char buf[4];
00244     unsigned char *c;
00245     int ret;
00246 
00247     c = buf + 4;
00248 
00249     if( ( ret = asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
00250         return( ret );
00251 
00252     ret = x509write_crt_set_extension( ctx, OID_NS_CERT_TYPE,
00253                                        OID_SIZE( OID_NS_CERT_TYPE ),
00254                                        0, buf, 4 );
00255     if( ret != 0 )
00256         return( ret );
00257 
00258     return( 0 );
00259 }
00260 
00261 static int x509_write_time( unsigned char **p, unsigned char *start,
00262                             const char *time, size_t size )
00263 {
00264     int ret;
00265     size_t len = 0;
00266 
00267     /*
00268      * write ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
00269      */
00270     if( time[0] == '2' && time[1] == '0' && time [2] < '5' )
00271     {
00272         ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
00273                                              (const unsigned char *) time + 2,
00274                                              size - 2 ) );
00275         ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
00276         ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_UTC_TIME ) );
00277     }
00278     else
00279     {
00280         ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
00281                                                   (const unsigned char *) time,
00282                                                   size ) );
00283         ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
00284         ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_GENERALIZED_TIME ) );
00285     }
00286 
00287     return( (int) len );
00288 }
00289 
00290 int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size,
00291                        int (*f_rng)(void *, unsigned char *, size_t),
00292                        void *p_rng )
00293 {
00294     int ret;
00295     const char *sig_oid;
00296     size_t sig_oid_len = 0;
00297     unsigned char *c, *c2;
00298     unsigned char hash[64];
00299     unsigned char sig[POLARSSL_MPI_MAX_SIZE];
00300     unsigned char tmp_buf[2048];
00301     size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
00302     size_t len = 0;
00303     pk_type_t pk_alg;
00304 
00305     /*
00306      * Prepare data to be signed in tmp_buf
00307      */
00308     c = tmp_buf + sizeof( tmp_buf );
00309 
00310     /* Signature algorithm needed in TBS, and later for actual signature */
00311     pk_alg = pk_get_type( ctx->issuer_key );
00312     if( pk_alg == POLARSSL_PK_ECKEY )
00313         pk_alg = POLARSSL_PK_ECDSA;
00314 
00315     if( ( ret = oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
00316                                         &sig_oid, &sig_oid_len ) ) != 0 )
00317     {
00318         return( ret );
00319     }
00320 
00321     /*
00322      *  Extensions  ::=  SEQUENCE SIZE (1..MAX) OF Extension
00323      */
00324     ASN1_CHK_ADD( len, x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
00325     ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
00326     ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
00327                                                     ASN1_SEQUENCE ) );
00328     ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
00329     ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC |
00330                                                     ASN1_CONSTRUCTED | 3 ) );
00331 
00332     /*
00333      *  SubjectPublicKeyInfo
00334      */
00335     ASN1_CHK_ADD( pub_len, pk_write_pubkey_der( ctx->subject_key,
00336                                                 tmp_buf, c - tmp_buf ) );
00337     c -= pub_len;
00338     len += pub_len;
00339 
00340     /*
00341      *  Subject  ::=  Name
00342      */
00343     ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->subject ) );
00344 
00345     /*
00346      *  Validity ::= SEQUENCE {
00347      *       notBefore      Time,
00348      *       notAfter       Time }
00349      */
00350     sub_len = 0;
00351 
00352     ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_after,
00353                                             X509_RFC5280_UTC_TIME_LEN ) );
00354 
00355     ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_before,
00356                                             X509_RFC5280_UTC_TIME_LEN ) );
00357 
00358     len += sub_len;
00359     ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
00360     ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
00361                                                     ASN1_SEQUENCE ) );
00362 
00363     /*
00364      *  Issuer  ::=  Name
00365      */
00366     ASN1_CHK_ADD( len, x509_write_names( &c, tmp_buf, ctx->issuer ) );
00367 
00368     /*
00369      *  Signature   ::=  AlgorithmIdentifier
00370      */
00371     ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, tmp_buf,
00372                        sig_oid, strlen( sig_oid ), 0 ) );
00373 
00374     /*
00375      *  Serial   ::=  INTEGER
00376      */
00377     ASN1_CHK_ADD( len, asn1_write_mpi( &c, tmp_buf, &ctx->serial ) );
00378 
00379     /*
00380      *  Version  ::=  INTEGER  {  v1(0), v2(1), v3(2)  }
00381      */
00382     sub_len = 0;
00383     ASN1_CHK_ADD( sub_len, asn1_write_int( &c, tmp_buf, ctx->version ) );
00384     len += sub_len;
00385     ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
00386     ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONTEXT_SPECIFIC |
00387                                                     ASN1_CONSTRUCTED | 0 ) );
00388 
00389     ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
00390     ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED |
00391                                                     ASN1_SEQUENCE ) );
00392 
00393     /*
00394      * Make signature
00395      */
00396     md( md_info_from_type( ctx->md_alg ), c, len, hash );
00397 
00398     if( ( ret = pk_sign( ctx->issuer_key, ctx->md_alg, hash, 0, sig, &sig_len,
00399                          f_rng, p_rng ) ) != 0 )
00400     {
00401         return( ret );
00402     }
00403 
00404     /*
00405      * Write data to output buffer
00406      */
00407     c2 = buf + size;
00408     ASN1_CHK_ADD( sig_and_oid_len, x509_write_sig( &c2, buf,
00409                                         sig_oid, sig_oid_len, sig, sig_len ) );
00410 
00411     c2 -= len;
00412     memcpy( c2, c, len );
00413 
00414     len += sig_and_oid_len;
00415     ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
00416     ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED |
00417                                                  ASN1_SEQUENCE ) );
00418 
00419     return( (int) len );
00420 }
00421 
00422 #define PEM_BEGIN_CRT           "-----BEGIN CERTIFICATE-----\n"
00423 #define PEM_END_CRT             "-----END CERTIFICATE-----\n"
00424 
00425 #if defined(POLARSSL_PEM_WRITE_C)
00426 int x509write_crt_pem( x509write_cert *crt, unsigned char *buf, size_t size,
00427                        int (*f_rng)(void *, unsigned char *, size_t),
00428                        void *p_rng )
00429 {
00430     int ret;
00431     unsigned char output_buf[4096];
00432     size_t olen = 0;
00433 
00434     if( ( ret = x509write_crt_der( crt, output_buf, sizeof(output_buf),
00435                                    f_rng, p_rng ) ) < 0 )
00436     {
00437         return( ret );
00438     }
00439 
00440     if( ( ret = pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
00441                                   output_buf + sizeof(output_buf) - ret,
00442                                   ret, buf, size, &olen ) ) != 0 )
00443     {
00444         return( ret );
00445     }
00446 
00447     return( 0 );
00448 }
00449 #endif /* POLARSSL_PEM_WRITE_C */
00450 
00451 #endif /* POLARSSL_X509_CRT_WRITE_C */
00452