- Added preliminary ASN.1 buffer writing support
 - Added preliminary X509 Certificate Request writing support
 - Added key_app_writer example application
 - Added cert_req example application

diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt
index de72735..bb600d4 100644
--- a/library/CMakeLists.txt
+++ b/library/CMakeLists.txt
@@ -4,6 +4,7 @@
      aes.c
      arc4.c
      asn1parse.c
+     asn1write.c
      base64.c
      bignum.c
      camellia.c
diff --git a/library/Makefile b/library/Makefile
index 26de4d7..968ca31 100644
--- a/library/Makefile
+++ b/library/Makefile
@@ -23,6 +23,7 @@
 # DLEXT=dll
 
 OBJS=	aes.o		arc4.o		asn1parse.o		\
+		asn1write.o								\
 		base64.o	bignum.o	camellia.o		\
 		certs.o		cipher.o	cipher_wrap.o	\
 		ctr_drbg.o	debug.o		des.o			\
@@ -34,7 +35,7 @@
 		rsa.o		sha1.o		sha2.o			\
 		sha4.o		ssl_cli.o	ssl_srv.o		\
 		ssl_tls.o	timing.o	version.o		\
-		x509parse.o	xtea.o
+		x509parse.o	x509write.o	xtea.o
 	
 
 .SILENT:
diff --git a/library/asn1write.c b/library/asn1write.c
new file mode 100644
index 0000000..0c1d18b
--- /dev/null
+++ b/library/asn1write.c
@@ -0,0 +1,218 @@
+/*
+ * ASN.1 buffer writing functionality
+ *
+ *  Copyright (C) 2006-2012, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "polarssl/config.h"
+
+#if defined(POLARSSL_ASN1_WRITE_C)
+
+#include "polarssl/asn1write.h"
+
+int asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
+{
+    if( len < 0x80 )
+    {
+        if( *p - start < 1 )
+            return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+
+        *--(*p) = len;
+        return( 1 );
+    }
+
+    if( len <= 0xFF )
+    {
+        if( *p - start < 2 )
+            return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+
+        *--(*p) = len;
+        *--(*p) = 0x81;
+        return( 2 );
+    }
+
+    if( *p - start < 3 )
+        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+
+    // We assume we never have lengths larger than 65535 bytes
+    //
+    *--(*p) = len % 256;
+    *--(*p) = ( len / 256 ) % 256;
+    *--(*p) = 0x82;
+
+    return( 3 );
+}
+
+int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
+{
+    if( *p - start < 1 )
+        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+
+    *--(*p) = tag;
+
+    return( 1 );
+}
+
+int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X )
+{
+    int ret;
+    size_t len = 0;
+
+    // Write the MPI
+    //
+    len = mpi_size( X );
+
+    if( *p - start < (int) len )
+        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+
+    (*p) -= len;
+    mpi_write_binary( X, *p, len );
+
+    // DER format assumes 2s complement for numbers, so the leftmost bit
+    // should be 0 for positive numbers and 1 for negative numbers.
+    //
+    if ( X->s ==1 && **p & 0x80 )
+    {
+        if( *p - start < 1 )
+            return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+
+        *--(*p) = 0x00;
+        len += 1;
+    }
+
+    ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
+
+    return( len );
+}
+    
+int asn1_write_null( unsigned char **p, unsigned char *start )
+{
+    int ret;
+    size_t len = 0;
+
+    // Write NULL
+    //
+    ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
+    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
+
+    return( len );
+}
+
+int asn1_write_oid( unsigned char **p, unsigned char *start, char *oid )
+{
+    int ret;
+    size_t len = 0;
+
+    // Write OID
+    //
+    len = strlen( oid );
+
+    if( *p - start < (int) len )
+        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+
+    (*p) -= len;
+    memcpy( *p, oid, len );
+
+    ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
+    ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
+
+    return( len );
+}
+
+int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
+                                     char *algorithm_oid )
+{
+    int ret;
+    size_t null_len = 0;
+    size_t oid_len = 0;
+    size_t len = 0;
+
+    // Write NULL
+    //
+    ASN1_CHK_ADD( null_len, asn1_write_null( p, start ) );
+
+    // Write OID
+    //
+    ASN1_CHK_ADD( oid_len, asn1_write_oid( p, start, algorithm_oid ) );
+
+    len = oid_len + null_len;
+    ASN1_CHK_ADD( len, asn1_write_len( p, start, oid_len + null_len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( p, start,
+                                       ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
+
+    return( len );
+}
+
+int asn1_write_int( unsigned char **p, unsigned char *start, int val )
+{
+    int ret;
+    size_t len = 0;
+
+    // TODO negative values and values larger than 128
+    // DER format assumes 2s complement for numbers, so the leftmost bit
+    // should be 0 for positive numbers and 1 for negative numbers.
+    //
+    if( *p - start < 1 )
+        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+
+    len += 1;
+    *--(*p) = val;
+
+    if ( val > 0 && **p & 0x80 )
+    {
+        if( *p - start < 1 )
+            return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+
+        *--(*p) = 0x00;
+        len += 1;
+    }
+
+    ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
+
+    return( len );
+}
+
+int asn1_write_printable_string( unsigned char **p, unsigned char *start,
+                                 char *text )
+{
+    int ret;
+    size_t len = 0;
+
+    // Write string
+    //
+    len = strlen( text );
+
+    if( *p - start < (int) len )
+        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+
+    (*p) -= len;
+    memcpy( *p, text, len );
+
+    ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
+
+    return( len );
+}
+    
+#endif
diff --git a/library/error.c b/library/error.c
index 9bc5034..195ae20 100644
--- a/library/error.c
+++ b/library/error.c
@@ -378,6 +378,8 @@
         snprintf( buf, buflen, "ASN1 - Data is invalid. (not used)" );
     if( use_ret == -(POLARSSL_ERR_ASN1_MALLOC_FAILED) )
         snprintf( buf, buflen, "ASN1 - Memory allocation failed" );
+    if( use_ret == -(POLARSSL_ERR_ASN1_BUF_TOO_SMALL) )
+        snprintf( buf, buflen, "ASN1 - Buffer too small when writing ASN.1 data structure" );
 #endif /* POLARSSL_ASN1_PARSE_C */
 
 #if defined(POLARSSL_BASE64_C)
diff --git a/library/x509write.c b/library/x509write.c
new file mode 100644
index 0000000..b114e70
--- /dev/null
+++ b/library/x509write.c
@@ -0,0 +1,233 @@
+/*
+ * X509 buffer writing functionality
+ *
+ *  Copyright (C) 2006-2012, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "polarssl/config.h"
+
+#if defined(POLARSSL_X509_WRITE_C)
+
+#include "polarssl/asn1write.h"
+#include "polarssl/x509write.h"
+#include "polarssl/x509.h"
+#include "polarssl/sha1.h"
+
+int x509_write_pubkey_der( unsigned char *buf, size_t size, rsa_context *rsa )
+{
+    int ret;
+    unsigned char *c;
+    size_t len = 0;
+
+    c = buf + size - 1;
+
+    ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->E ) );
+    ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->N ) );
+
+    ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
+
+    if( c - buf < 1 )
+        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+
+    *--c = 0;
+    len += 1;
+
+    ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_BIT_STRING ) );
+
+    ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, buf, OID_PKCS1_RSA ) );
+
+    ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
+
+    return( len );
+}
+
+int x509_write_key_der( unsigned char *buf, size_t size, rsa_context *rsa )
+{
+    int ret;
+    unsigned char *c;
+    size_t len = 0;
+
+    c = buf + size - 1;
+
+    ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->QP ) );
+    ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->DQ ) );
+    ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->DP ) );
+    ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->Q ) );
+    ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->P ) );
+    ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->D ) );
+    ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->E ) );
+    ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->N ) );
+    ASN1_CHK_ADD( len, asn1_write_int( &c, buf, 0 ) );
+
+    ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
+
+    // TODO: Make NON RSA Specific variant later on
+/*    *--c = 0;
+    len += 1;
+
+    len += asn1_write_len( &c, len);
+    len += asn1_write_tag( &c, ASN1_BIT_STRING );
+
+    len += asn1_write_oid( &c, OID_PKCS1_RSA );
+
+    len += asn1_write_int( &c, 0 );
+
+    len += asn1_write_len( &c, len);
+    len += asn1_write_tag( &c, ASN1_CONSTRUCTED | ASN1_SEQUENCE );*/
+
+/*    for(i = 0; i < len; ++i)
+    {
+        if (i % 16 == 0 ) printf("\n");
+        printf("%02x ", c[i]);
+    }
+    printf("\n");*/
+
+    return( len );
+}
+
+int x509_write_name( unsigned char **p, unsigned char *start, char *oid,
+                     char *name )
+{
+    int ret;
+    size_t string_len = 0;
+    size_t oid_len = 0;
+    size_t len = 0;
+
+    // Write PrintableString
+    //
+    ASN1_CHK_ADD( string_len, asn1_write_printable_string( p, start, name ) );
+
+    // Write OID
+    //
+    ASN1_CHK_ADD( oid_len, asn1_write_oid( p, start, oid ) );
+
+    len = oid_len + string_len;
+    ASN1_CHK_ADD( len, asn1_write_len( p, start, oid_len + string_len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
+
+    ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | ASN1_SET ) );
+
+    return( len );
+}
+
+int x509_write_sig( unsigned char **p, unsigned char *start, char *oid,
+                    unsigned char *sig, size_t size )
+{
+    int ret;
+    size_t len = 0;
+
+    if( *p - start < (int) size + 1 )
+        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+
+    len = size;
+    (*p) -= len;
+    memcpy( *p, sig, len );
+
+    *--(*p) = 0;
+    len += 1;
+
+    ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
+
+    // Write OID
+    //
+    ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid ) );
+
+    return( len );
+}
+
+int x509_write_cert_req( unsigned char *buf, size_t size, rsa_context *rsa,
+                         x509_req_name *req_name )
+{
+    int ret;
+    unsigned char *c, *c2;
+    unsigned char hash[20];
+    unsigned char sig[512];
+    unsigned char tmp_buf[2048];
+    size_t sub_len = 0, pub_len = 0, sig_len = 0;
+    size_t len = 0;
+    x509_req_name *cur = req_name;
+
+    c = tmp_buf + 2048 - 1;
+
+    ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, 0 ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC ) );
+
+    ASN1_CHK_ADD( pub_len, asn1_write_mpi( &c, tmp_buf, &rsa->E ) );
+    ASN1_CHK_ADD( pub_len, asn1_write_mpi( &c, tmp_buf, &rsa->N ) );
+
+    ASN1_CHK_ADD( pub_len, asn1_write_len( &c, tmp_buf, pub_len ) );
+    ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
+
+    if( c - tmp_buf < 1 )
+        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+
+    *--c = 0;
+    pub_len += 1;
+
+    ASN1_CHK_ADD( pub_len, asn1_write_len( &c, tmp_buf, pub_len ) );
+    ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, tmp_buf, ASN1_BIT_STRING ) );
+
+    ASN1_CHK_ADD( pub_len, asn1_write_algorithm_identifier( &c, tmp_buf, OID_PKCS1_RSA ) );
+
+    len += pub_len;
+    ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, pub_len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
+
+    while( cur != NULL )
+    {
+        ASN1_CHK_ADD( sub_len, x509_write_name( &c, tmp_buf, cur->oid, cur->name ) );
+        
+        cur = cur->next;
+    }
+
+    len += sub_len;
+    ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, sub_len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
+
+    ASN1_CHK_ADD( len, asn1_write_int( &c, tmp_buf, 0 ) );
+
+    ASN1_CHK_ADD( len, asn1_write_len( &c, tmp_buf, len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( &c, tmp_buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
+    
+    sha1( c, len, hash );
+    rsa_pkcs1_sign( rsa, NULL, NULL, RSA_PRIVATE, SIG_RSA_SHA1, 0, hash, sig );
+
+    c2 = buf + size - 1;
+    ASN1_CHK_ADD( sig_len, x509_write_sig( &c2, buf, OID_PKCS1_SHA1, sig, rsa->len ) );
+    
+    c2 -= len;
+    memcpy( c2, c, len ); 
+    
+    len += sig_len;
+    ASN1_CHK_ADD( len, asn1_write_len( &c2, buf, len ) );
+    ASN1_CHK_ADD( len, asn1_write_tag( &c2, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
+
+    return( len );
+}
+
+#endif