pk_set_type() cannot be used to reset key type
diff --git a/library/pk.c b/library/pk.c
index 71505ed..0591b3f 100644
--- a/library/pk.c
+++ b/library/pk.c
@@ -88,6 +88,12 @@
 {
     size_t size = 0;
 
+    if( ctx->type == type )
+        return( 0 );
+
+    if( ctx->type != POLARSSL_PK_NONE )
+        return( POLARSSL_ERR_PK_TYPE_MISMATCH );
+
     switch( type )
     {
 #if defined(POLARSSL_RSA_C)
@@ -104,7 +110,7 @@
 #endif
 
         case POLARSSL_PK_NONE:
-            ; /* Should not happen */
+            ; /* Cannot happen, but the cmpiler doesn't know */
     }
 
     if( ( ctx->data = malloc( size ) ) == NULL )