Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

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