- Certificate Requests written now have the Email address written in IA5String


diff --git a/include/polarssl/asn1.h b/include/polarssl/asn1.h
index dc3f2fe..893292d 100644
--- a/include/polarssl/asn1.h
+++ b/include/polarssl/asn1.h
@@ -53,6 +53,8 @@
 #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 */
+#define POLARSSL_ERR_ASN1_BUF_TOO_SMALL                    -0x006C  /**< Buffer too small when writing ASN.1 data structure. */
+
 /* \} name */
 
 /**
diff --git a/include/polarssl/asn1write.h b/include/polarssl/asn1write.h
index 38f6eea..52b9baa 100644
--- a/include/polarssl/asn1write.h
+++ b/include/polarssl/asn1write.h
@@ -29,8 +29,6 @@
 
 #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 );
@@ -42,5 +40,7 @@
 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 );
+int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
+                                 char *text );
 
 #endif /* POLARSSL_ASN1_WRITE_H */
diff --git a/library/asn1write.c b/library/asn1write.c
index 0c1d18b..e50c17c 100644
--- a/library/asn1write.c
+++ b/library/asn1write.c
@@ -215,4 +215,27 @@
     return( len );
 }
     
+int asn1_write_ia5_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_IA5_STRING ) );
+
+    return( len );
+}
+    
+
 #endif
diff --git a/library/x509write.c b/library/x509write.c
index b114e70..2b021d7 100644
--- a/library/x509write.c
+++ b/library/x509write.c
@@ -116,9 +116,15 @@
     size_t oid_len = 0;
     size_t len = 0;
 
-    // Write PrintableString
+    // Write PrintableString for all except OID_PKCS9_EMAIL
     //
-    ASN1_CHK_ADD( string_len, asn1_write_printable_string( p, start, name ) );
+    if( OID_SIZE( OID_PKCS9_EMAIL ) == strlen( oid ) &&
+        memcmp( oid, OID_PKCS9_EMAIL, strlen( oid ) ) == 0 )
+    {
+        ASN1_CHK_ADD( string_len, asn1_write_ia5_string( p, start, name ) );
+    }
+    else
+        ASN1_CHK_ADD( string_len, asn1_write_printable_string( p, start, name ) );
 
     // Write OID
     //