Fix key_app_writer
diff --git a/programs/pkey/key_app_writer.c b/programs/pkey/key_app_writer.c
index d9ab45c..371b03a 100644
--- a/programs/pkey/key_app_writer.c
+++ b/programs/pkey/key_app_writer.c
@@ -33,21 +33,16 @@
 
 #include "polarssl/config.h"
 
-#include "polarssl/error.h"
-#include "polarssl/rsa.h"
-#include "polarssl/x509.h"
-#include "polarssl/base64.h"
 #include "polarssl/x509write.h"
+#include "polarssl/error.h"
 
-#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) ||         \
-    !defined(POLARSSL_X509_WRITE_C) || !defined(POLARSSL_FS_IO)
+#if !defined(POLARSSL_X509_WRITE_C) || !defined(POLARSSL_FS_IO)
 int main( int argc, char *argv[] )
 {
     ((void) argc);
     ((void) argv);
 
-    printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
-           "POLARSSL_X509_WRITE_C and/or POLARSSL_FS_IO not defined.\n");
+    printf( "POLARSSL_X509_WRITE_C and/or POLARSSL_FS_IO not defined.\n" );
     return( 0 );
 }
 #else
@@ -82,7 +77,7 @@
     int output_format;          /* the output format to use             */
 } opt;
 
-static int write_public_key( rsa_context *rsa, const char *output_file )
+static int write_public_key( pk_context *key, const char *output_file )
 {
     int ret;
     FILE *f;
@@ -94,14 +89,14 @@
 
     if( opt.output_format == OUTPUT_FORMAT_PEM )
     {
-        if( ( ret = x509write_pubkey_pem( rsa, output_buf, 16000 ) ) != 0 )
+        if( ( ret = x509write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
             return( ret );
 
         len = strlen( (char *) output_buf );
     }
     else
     {
-        if( ( ret = x509write_pubkey_der( rsa, output_buf, 16000 ) ) < 0 )
+        if( ( ret = x509write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
             return( ret );
 
         len = ret;
@@ -119,7 +114,7 @@
     return( 0 );
 }
 
-static int write_private_key( rsa_context *rsa, const char *output_file )
+static int write_private_key( pk_context *key, const char *output_file )
 {
     int ret;
     FILE *f;
@@ -130,14 +125,14 @@
     memset(output_buf, 0, 16000);
     if( opt.output_format == OUTPUT_FORMAT_PEM )
     {
-        if( ( ret = x509write_key_pem( rsa, output_buf, 16000 ) ) != 0 )
+        if( ( ret = x509write_key_pem( key, output_buf, 16000 ) ) != 0 )
             return( ret );
 
         len = strlen( (char *) output_buf );
     }
     else
     {
-        if( ( ret = x509write_key_der( rsa, output_buf, 16000 ) ) < 0 )
+        if( ( ret = x509write_key_der( key, output_buf, 16000 ) ) < 0 )
             return( ret );
 
         len = ret;
@@ -168,7 +163,7 @@
 int main( int argc, char *argv[] )
 {
     int ret = 0;
-    rsa_context rsa;
+    pk_context key;
     char buf[1024];
     int i;
     char *p, *q;
@@ -176,12 +171,13 @@
     /*
      * Set to sane values
      */
-    memset( &rsa, 0, sizeof( rsa_context ) );
-    memset( buf, 0, 1024 );
+    pk_init( &key );
+    memset( buf, 0, sizeof( buf ) );
 
     if( argc == 0 )
     {
     usage:
+        ret = 1;
         printf( USAGE );
         goto exit;
     }
@@ -254,15 +250,11 @@
         printf( "\n  . Loading the private key ..." );
         fflush( stdout );
 
-        ret = x509parse_keyfile_rsa( &rsa, opt.filename, NULL );
+        ret = x509parse_keyfile( &key, opt.filename, NULL );
 
         if( ret != 0 )
         {
-#ifdef POLARSSL_ERROR_C
-            polarssl_strerror( ret, buf, 1024 );
-#endif
-            printf( " failed\n  !  x509parse_key_rsa returned %d - %s\n\n", ret, buf );
-            rsa_free( &rsa );
+            printf( " failed\n  !  x509parse_key returned %d", ret );
             goto exit;
         }
 
@@ -272,14 +264,23 @@
          * 1.2 Print the key
          */
         printf( "  . Key information    ...\n" );
-        mpi_write_file( "N:  ", &rsa.N, 16, NULL );
-        mpi_write_file( "E:  ", &rsa.E, 16, NULL );
-        mpi_write_file( "D:  ", &rsa.D, 16, NULL );
-        mpi_write_file( "P:  ", &rsa.P, 16, NULL );
-        mpi_write_file( "Q:  ", &rsa.Q, 16, NULL );
-        mpi_write_file( "DP: ", &rsa.DP, 16, NULL );
-        mpi_write_file( "DQ:  ", &rsa.DQ, 16, NULL );
-        mpi_write_file( "QP:  ", &rsa.QP, 16, NULL );
+
+#if defined(POLARSSL_RSA_C)
+        if( pk_get_type( &key ) == POLARSSL_PK_RSA )
+        {
+            rsa_context *rsa = pk_rsa( key );
+            mpi_write_file( "N:  ",  &rsa->N,  16, NULL );
+            mpi_write_file( "E:  ",  &rsa->E,  16, NULL );
+            mpi_write_file( "D:  ",  &rsa->D,  16, NULL );
+            mpi_write_file( "P:  ",  &rsa->P,  16, NULL );
+            mpi_write_file( "Q:  ",  &rsa->Q,  16, NULL );
+            mpi_write_file( "DP: ",  &rsa->DP, 16, NULL );
+            mpi_write_file( "DQ:  ", &rsa->DQ, 16, NULL );
+            mpi_write_file( "QP:  ", &rsa->QP, 16, NULL );
+        }
+        else
+#endif
+            printf("key type not supported yet\n");
 
     }
     else if( opt.mode == MODE_PUBLIC )
@@ -290,15 +291,11 @@
         printf( "\n  . Loading the public key ..." );
         fflush( stdout );
 
-        ret = x509parse_public_keyfile_rsa( &rsa, opt.filename );
+        ret = x509parse_public_keyfile( &key, opt.filename );
 
         if( ret != 0 )
         {
-#ifdef POLARSSL_ERROR_C
-            polarssl_strerror( ret, buf, 1024 );
-#endif
-            printf( " failed\n  !  x509parse_public_key_rsa returned %d - %s\n\n", ret, buf );
-            rsa_free( &rsa );
+            printf( " failed\n  !  x509parse_public_key returned %d", ret );
             goto exit;
         }
 
@@ -308,24 +305,43 @@
          * 1.2 Print the key
          */
         printf( "  . Key information    ...\n" );
-        mpi_write_file( "N: ", &rsa.N, 16, NULL );
-        mpi_write_file( "E:  ", &rsa.E, 16, NULL );
+
+#if defined(POLARSSL_RSA_C)
+        if( pk_get_type( &key ) == POLARSSL_PK_RSA )
+        {
+            rsa_context *rsa = pk_rsa( key );
+            mpi_write_file( "N: ", &rsa->N, 16, NULL );
+            mpi_write_file( "E: ", &rsa->E, 16, NULL );
+        }
+        else
+#endif
+            printf("key type not supported yet\n");
     }
     else
         goto usage;
 
     if( opt.output_mode == OUTPUT_MODE_PUBLIC )
     {
-        write_public_key( &rsa, opt.output_file );
+        write_public_key( &key, opt.output_file );
     }
     if( opt.output_mode == OUTPUT_MODE_PRIVATE )
     {
-        write_private_key( &rsa, opt.output_file );
+        write_private_key( &key, opt.output_file );
     }
 
 exit:
 
-    rsa_free( &rsa );
+    if( ret != 0 && ret != 1)
+    {
+#ifdef POLARSSL_ERROR_C
+        polarssl_strerror( ret, buf, sizeof( buf ) );
+        printf( " - %s\n", buf );
+#else
+        printf("\n");
+#endif
+    }
+
+    pk_free( &key );
 
 #if defined(_WIN32)
     printf( "  + Press Enter to exit this program.\n" );
@@ -334,5 +350,4 @@
 
     return( ret );
 }
-#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
-          POLARSSL_X509_WRITE_C && POLARSSL_FS_IO */
+#endif /* POLARSSL_X509_WRITE_C && POLARSSL_FS_IO */