Nicer interface between PK and debug.

Finally get rid of pk_context.type member, too.
diff --git a/library/debug.c b/library/debug.c
index 8e3dd03..5522fb6 100644
--- a/library/debug.c
+++ b/library/debug.c
@@ -225,6 +225,39 @@
 #endif /* POLARSSL_BIGNUM_C */
 
 #if defined(POLARSSL_X509_PARSE_C)
+static void debug_print_pk( const ssl_context *ssl, int level,
+                            const char *file, int line,
+                            const char *text, const pk_context *pk )
+{
+    size_t i;
+    pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
+    char name[16];
+
+    memset( items, 0, sizeof( items ) );
+
+    if( pk_debug( pk, items ) != 0 )
+    {
+        debug_print_msg( ssl, level, file, line, "invalid PK context" );
+        return;
+    }
+
+    for( i = 0; i < sizeof( items ); i++ )
+    {
+        if( items[i].type == POLARSSL_PK_DEBUG_NONE )
+            return;
+
+        snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
+        name[sizeof( name ) - 1] = '\0';
+
+        if( items[i].type == POLARSSL_PK_DEBUG_MPI )
+            debug_print_mpi( ssl, level, file, line, name, items[i].value );
+        else if( items[i].type == POLARSSL_PK_DEBUG_ECP )
+            debug_print_ecp( ssl, level, file, line, name, items[i].value );
+        else
+            debug_print_msg( ssl, level, file, line, "should not happen" );
+    }
+}
+
 void debug_print_crt( const ssl_context *ssl, int level,
                       const char *file, int line,
                       const char *text, const x509_cert *crt )
@@ -250,25 +283,7 @@
         str[maxlen] = '\0';
         ssl->f_dbg( ssl->p_dbg, level, str );
 
-#if defined(POLARSSL_RSA_C)
-        if( crt->pk.type == POLARSSL_PK_RSA )
-        {
-            debug_print_mpi( ssl, level, file, line,
-                    "crt->rsa.N", &pk_rsa( crt->pk )->N );
-            debug_print_mpi( ssl, level, file, line,
-                    "crt->rsa.E", &pk_rsa( crt->pk )->E );
-        } else
-#endif /* POLARSSL_RSA_C */
-#if defined(POLARSSL_ECP_C)
-        if( crt->pk.type == POLARSSL_PK_ECKEY ||
-            crt->pk.type == POLARSSL_PK_ECKEY_DH )
-        {
-            debug_print_ecp( ssl, level, file, line,
-                    "crt->eckey.Q", &pk_ec( crt->pk )->Q );
-        } else
-#endif /* POLARSSL_ECP_C */
-            debug_print_msg( ssl, level, file, line,
-                    "crt->pk.type is not valid" );
+        debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
 
         crt = crt->next;
     }
diff --git a/library/pk.c b/library/pk.c
index ce3b88a..f3c64cb 100644
--- a/library/pk.c
+++ b/library/pk.c
@@ -56,7 +56,6 @@
         return;
 
     ctx->info = NULL;
-    ctx->type = POLARSSL_PK_NONE;
     ctx->data = NULL;
 }
 
@@ -72,7 +71,6 @@
     ctx->data = NULL;
 
     ctx->info = NULL;
-    ctx->type = POLARSSL_PK_NONE;
 }
 
 /*
@@ -107,11 +105,13 @@
 {
     const pk_info_t *info;
 
-    if( ctx->type == type )
-        return( 0 );
+    if( ctx->info != NULL )
+    {
+        if( ctx->info->type == type )
+            return 0;
 
-    if( ctx->type != POLARSSL_PK_NONE )
         return( POLARSSL_ERR_PK_TYPE_MISMATCH );
+    }
 
     if( ( info = pk_info_from_type( type ) ) == NULL )
         return( POLARSSL_ERR_PK_TYPE_MISMATCH );
@@ -119,7 +119,6 @@
     if( ( ctx->data = info->ctx_alloc_func() ) == NULL )
         return( POLARSSL_ERR_PK_MALLOC_FAILED );
 
-    ctx->type = type;
     ctx->info = info;
 
     return( 0 );
@@ -160,3 +159,15 @@
 
     return( ctx->info->get_size( ctx->data ) );
 }
+
+/*
+ * Export debug information
+ */
+int pk_debug( const pk_context *ctx, pk_debug_item *items )
+{
+    if( ctx == NULL || ctx->info == NULL )
+        return( POLARSSL_ERR_PK_TYPE_MISMATCH ); // TODO
+
+    ctx->info->debug_func( ctx->data, items );
+    return( 0 );
+}
diff --git a/library/pk_wrap.c b/library/pk_wrap.c
index 239ff78..284bd1d 100644
--- a/library/pk_wrap.c
+++ b/library/pk_wrap.c
@@ -84,6 +84,19 @@
     polarssl_free( ctx );
 }
 
+static void rsa_debug( const void *ctx, pk_debug_item *items )
+{
+    items->type = POLARSSL_PK_DEBUG_MPI;
+    items->name = "rsa.N";
+    items->value = &( ((rsa_context *) ctx)->N );
+
+    items++;
+
+    items->type = POLARSSL_PK_DEBUG_MPI;
+    items->name = "rsa.E";
+    items->value = &( ((rsa_context *) ctx)->E );
+}
+
 const pk_info_t rsa_info = {
     POLARSSL_PK_RSA,
     "RSA",
@@ -92,6 +105,7 @@
     rsa_verify_wrap,
     rsa_alloc_wrap,
     rsa_free_wrap,
+    rsa_debug,
 };
 #endif /* POLARSSL_RSA_C */
 
@@ -138,6 +152,7 @@
     ecdsa_verify_wrap,
     ecdsa_alloc_wrap,
     ecdsa_free_wrap,
+    NULL,
 };
 #endif /* POLARSSL_ECDSA_C */
 
@@ -200,6 +215,13 @@
     polarssl_free( ctx );
 }
 
+static void eckey_debug( const void *ctx, pk_debug_item *items )
+{
+    items->type = POLARSSL_PK_DEBUG_ECP;
+    items->name = "eckey.Q";
+    items->value = &( ((ecp_keypair *) ctx)->Q );
+}
+
 const pk_info_t eckey_info = {
     POLARSSL_PK_ECKEY,
     "EC",
@@ -208,6 +230,7 @@
     eckey_verify_wrap,
     eckey_alloc_wrap,
     eckey_free_wrap,
+    eckey_debug,
 };
 
 /*
@@ -240,5 +263,6 @@
     eckeydh_verify_wrap,
     eckey_alloc_wrap,       /* Same underlying key structure */
     eckey_free_wrap,        /* Same underlying key structure */
+    NULL,
 };
 #endif /* POLARSSL_ECP_C */