- 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/include/polarssl/asn1.h b/include/polarssl/asn1.h
index bc7b6bb..dc3f2fe 100644
--- a/include/polarssl/asn1.h
+++ b/include/polarssl/asn1.h
@@ -47,12 +47,12 @@
  * ASN1 is a standard to specify data structures.
  * \{
  */
-#define POLARSSL_ERR_ASN1_OUT_OF_DATA                      -0x0014  /**< Out of data when parsing an ASN1 data structure. */
-#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG                   -0x0016  /**< ASN1 tag was of an unexpected value. */
-#define POLARSSL_ERR_ASN1_INVALID_LENGTH                   -0x0018  /**< Error when trying to determine the length or invalid length. */
-#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH                  -0x001A  /**< Actual length differs from expected length. */
-#define POLARSSL_ERR_ASN1_INVALID_DATA                     -0x001C  /**< Data is invalid. (not used) */
-#define POLARSSL_ERR_ASN1_MALLOC_FAILED                    -0x001E  /**< Memory allocation failed */
+#define POLARSSL_ERR_ASN1_OUT_OF_DATA                      -0x0060  /**< Out of data when parsing an ASN1 data structure. */
+#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG                   -0x0062  /**< ASN1 tag was of an unexpected value. */
+#define POLARSSL_ERR_ASN1_INVALID_LENGTH                   -0x0064  /**< Error when trying to determine the length or invalid length. */
+#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH                  -0x0066  /**< Actual length differs from expected length. */
+#define POLARSSL_ERR_ASN1_INVALID_DATA                     -0x0068  /**< Data is invalid. (not used) */
+#define POLARSSL_ERR_ASN1_MALLOC_FAILED                    -0x006A  /**< Memory allocation failed */
 /* \} name */
 
 /**
diff --git a/include/polarssl/asn1write.h b/include/polarssl/asn1write.h
new file mode 100644
index 0000000..38f6eea
--- /dev/null
+++ b/include/polarssl/asn1write.h
@@ -0,0 +1,46 @@
+/**
+ * \file asn1write.h
+ *
+ * \brief 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.
+ */
+#ifndef POLARSSL_ASN1_WRITE_H
+#define POLARSSL_ASN1_WRITE_H
+
+#include "asn1.h"
+
+#define POLARSSL_ERR_ASN1_BUF_TOO_SMALL                    -0x006C  /**< Buffer too small when writing ASN.1 data structure. */
+
+#define ASN1_CHK_ADD(g, f) if( ( ret = f ) < 0 ) return( ret ); else g += ret
+
+int asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
+int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag );
+int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X );
+int asn1_write_null( unsigned char **p, unsigned char *start );
+int asn1_write_oid( unsigned char **p, unsigned char *start, char *oid );
+int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start, char *algorithm_oid );
+int asn1_write_int( unsigned char **p, unsigned char *start, int val );
+int asn1_write_printable_string( unsigned char **p, unsigned char *start,
+                                 char *text );
+
+#endif /* POLARSSL_ASN1_WRITE_H */
diff --git a/include/polarssl/config.h b/include/polarssl/config.h
index bb9e680..62663ce 100644
--- a/include/polarssl/config.h
+++ b/include/polarssl/config.h
@@ -289,6 +289,15 @@
 #define POLARSSL_ASN1_PARSE_C
 
 /**
+ * \def POLARSSL_ASN1_WRITE_C
+ *
+ * Enable the generic ASN1 writer.
+ *
+ * Module:  library/asn1write.c
+ */
+#define POLARSSL_ASN1_WRITE_C
+
+/**
  * \def POLARSSL_BASE64_C
  *
  * Enable the Base64 module.
@@ -698,6 +707,19 @@
 #define POLARSSL_X509_PARSE_C
 
 /**
+ * \def POLARSSL_X509_WRITE_C
+ *
+ * Enable X.509 buffer writing.
+ *
+ * Module:  library/x509write.c
+ *
+ * Requires: POLARSSL_BIGNUM_C, POLARSSL_RSA_C
+ *
+ * This module is required for X.509 certificate request writing.
+ */
+#define POLARSSL_X509_WRITE_C
+
+/**
  * \def POLARSSL_XTEA_C
  *
  * Enable the XTEA block cipher.
diff --git a/include/polarssl/error.h b/include/polarssl/error.h
index 9c17071..e2e91f9 100644
--- a/include/polarssl/error.h
+++ b/include/polarssl/error.h
@@ -49,16 +49,16 @@
  *
  * Module   Nr  Codes assigned 
  * MPI       7  0x0002-0x0010
- * ASN1      6  0x0014-0x001E
  * AES       2  0x0020-0x0022
  * CAMELLIA  2  0x0024-0x0026
  * XTEA      1  0x0028-0x0028
  * BASE64    2  0x002A-0x002C
  * PADLOCK   1  0x0030-0x0030
  * DES       1  0x0032-0x0032
- * NET      11  0x0040-0x0054
  * CTR_DBRG  3  0x0034-0x003A
  * ENTROPY   3  0x003C-0x0040
+ * NET      11  0x0042-0x0056
+ * ASN1      7  0x0060-0x006C
  * MD2       1  0x0070-0x0070
  * MD4       1  0x0072-0x0072
  * MD5       1  0x0074-0x0074
diff --git a/include/polarssl/net.h b/include/polarssl/net.h
index 89e3a00..88302ac 100644
--- a/include/polarssl/net.h
+++ b/include/polarssl/net.h
@@ -29,7 +29,7 @@
 
 #include <string.h>
 
-#define POLARSSL_ERR_NET_UNKNOWN_HOST                      -0x0040  /**< Failed to get an IP address for the given hostname. */
+#define POLARSSL_ERR_NET_UNKNOWN_HOST                      -0x0056  /**< Failed to get an IP address for the given hostname. */
 #define POLARSSL_ERR_NET_SOCKET_FAILED                     -0x0042  /**< Failed to open a socket. */
 #define POLARSSL_ERR_NET_CONNECT_FAILED                    -0x0044  /**< The connection to the given server / port failed. */
 #define POLARSSL_ERR_NET_BIND_FAILED                       -0x0046  /**< Binding of the socket failed. */
diff --git a/include/polarssl/x509.h b/include/polarssl/x509.h
index 385c8b5..2cd883f 100644
--- a/include/polarssl/x509.h
+++ b/include/polarssl/x509.h
@@ -99,9 +99,15 @@
 
 #define OID_X520                "\x55\x04"
 #define OID_CN                  OID_X520 "\x03"
+#define OID_COUNTRY             OID_X520 "\x06"
+#define OID_LOCALITY            OID_X520 "\x07"
+#define OID_STATE               OID_X520 "\x08"
+#define OID_ORGANIZATION        OID_X520 "\x0A"
+#define OID_ORG_UNIT            OID_X520 "\x0B"
 
 #define OID_PKCS1               "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
 #define OID_PKCS1_RSA           OID_PKCS1 "\x01"
+#define OID_PKCS1_SHA1          OID_PKCS1 "\x05"
 
 #define OID_RSA_SHA_OBS         "\x2B\x0E\x03\x02\x1D"
 
diff --git a/include/polarssl/x509write.h b/include/polarssl/x509write.h
new file mode 100644
index 0000000..c24f3e1
--- /dev/null
+++ b/include/polarssl/x509write.h
@@ -0,0 +1,46 @@
+/**
+ * \file x509write.h
+ *
+ * \brief 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.
+ */
+#ifndef POLARSSL_X509_WRITE_H
+#define POLARSSL_X509_WRITE_H
+
+#include "rsa.h"
+
+typedef struct _x509_req_name
+{
+    char oid[128];
+    char name[128];
+
+    struct _x509_req_name *next;
+}
+x509_req_name;
+
+int x509_write_pubkey_der( unsigned char *buf, size_t size, rsa_context *rsa );
+int x509_write_key_der( unsigned char *buf, size_t size, rsa_context *rsa );
+int x509_write_cert_req( unsigned char *buf, size_t size, rsa_context *rsa,
+                         x509_req_name *req_name );
+
+#endif /* POLARSSL_X509_WRITE_H */