Get rid of pk_wrap_rsa()
diff --git a/include/polarssl/pk.h b/include/polarssl/pk.h
index 43b9f09..8626b60 100644
--- a/include/polarssl/pk.h
+++ b/include/polarssl/pk.h
@@ -112,7 +112,6 @@
     const pk_info_t *   info;       /**< Public key informations */
     pk_type_t           type;       /**< Public key type (temporary) */
     void *              data;       /**< Public key data */
-    int                 dont_free;  /**< True if data must not be freed */
 } pk_context;
 
 /**
@@ -140,21 +139,6 @@
  */
 int pk_set_type( pk_context *ctx, pk_type_t type );
 
-#if defined(POLARSSL_RSA_C)
-/**
- * \brief           Wrap a RSA context in a PK context
- *
- * \param ctx       PK context to initiliaze
- * \param rsa       RSA context to use
- *
- * \note            The PK context must be freshly initialized.
- *
- * \return          O on success,
- *                  POLARSSL_ERR_PK_TYPE_MISMATCH if ctx was not empty.
- */
-int pk_wrap_rsa( pk_context *ctx, const rsa_context *rsa);
-#endif /* POLARSSL_RSA_C */
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/include/polarssl/rsa.h b/include/polarssl/rsa.h
index a513a77..8e52e7d 100644
--- a/include/polarssl/rsa.h
+++ b/include/polarssl/rsa.h
@@ -507,6 +507,17 @@
                            const unsigned char *sig );
 
 /**
+ * \brief          Copy the components of an RSA context
+ *
+ * \param dst      Destination context
+ * \param src      Source context
+ *
+ * \return         O on success,
+ *                 POLARSSL_ERR_MPI_MALLOC_FAILED on memory allocation failure
+ */
+int rsa_copy( rsa_context *dst, const rsa_context *src );
+
+/**
  * \brief          Free the components of an RSA key
  *
  * \param ctx      RSA Context to free
diff --git a/library/pk.c b/library/pk.c
index c83d02b..19bc79b 100644
--- a/library/pk.c
+++ b/library/pk.c
@@ -58,7 +58,6 @@
     ctx->info = NULL;
     ctx->type = POLARSSL_PK_NONE;
     ctx->data = NULL;
-    ctx->dont_free = 0;
 }
 
 /*
@@ -88,8 +87,7 @@
         ; /* guard for the else's above */
     }
 
-    if( ! ctx->dont_free )
-        polarssl_free( ctx->data );
+    polarssl_free( ctx->data );
 
     ctx->info = NULL;
     ctx->type = POLARSSL_PK_NONE;
@@ -150,20 +148,3 @@
 
     return( 0 );
 }
-
-#if defined(POLARSSL_RSA_C)
-/*
- * Wrap an RSA context in a PK context
- */
-int pk_wrap_rsa( pk_context *ctx, const rsa_context *rsa)
-{
-    if( ctx->type != POLARSSL_PK_NONE )
-        return( POLARSSL_ERR_PK_TYPE_MISMATCH );
-
-    ctx->type = POLARSSL_PK_RSA;
-    ctx->data = (rsa_context *) rsa;
-    ctx->dont_free = 1;
-
-    return( 0 );
-}
-#endif
diff --git a/library/rsa.c b/library/rsa.c
index 8a9b0f4..ccdd048 100644
--- a/library/rsa.c
+++ b/library/rsa.c
@@ -1197,6 +1197,40 @@
 }
 
 /*
+ * Copy the components of an RSA key
+ */
+int rsa_copy( rsa_context *dst, const rsa_context *src )
+{
+    int ret;
+
+    dst->ver = src->ver;
+    dst->len = src->len;
+
+    MPI_CHK( mpi_copy( &dst->N, &src->N ) );
+    MPI_CHK( mpi_copy( &dst->E, &src->E ) );
+
+    MPI_CHK( mpi_copy( &dst->D, &src->D ) );
+    MPI_CHK( mpi_copy( &dst->P, &src->P ) );
+    MPI_CHK( mpi_copy( &dst->Q, &src->Q ) );
+    MPI_CHK( mpi_copy( &dst->DP, &src->DP ) );
+    MPI_CHK( mpi_copy( &dst->DQ, &src->DQ ) );
+    MPI_CHK( mpi_copy( &dst->QP, &src->QP ) );
+
+    MPI_CHK( mpi_copy( &dst->RN, &src->RN ) );
+    MPI_CHK( mpi_copy( &dst->RP, &src->RP ) );
+    MPI_CHK( mpi_copy( &dst->RQ, &src->RQ ) );
+
+    dst->padding = src->padding;
+    dst->hash_id = src->padding;
+
+cleanup:
+    if( ret != 0 )
+        rsa_free( dst );
+
+    return( ret );
+}
+
+/*
  * Free the components of an RSA key
  */
 void rsa_free( rsa_context *ctx )
diff --git a/library/x509parse.c b/library/x509parse.c
index 8248373..a8fcc0b 100644
--- a/library/x509parse.c
+++ b/library/x509parse.c
@@ -2138,12 +2138,22 @@
  */
 int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
 {
+    int ret;
     pk_context pk;
 
     pk_init( &pk );
-    pk_wrap_rsa( &pk, rsa );
+    pk_set_type( &pk, POLARSSL_PK_RSA );
 
-    return( x509parse_keyfile( &pk, path, pwd ) );
+    ret = x509parse_keyfile( &pk, path, pwd );
+
+    if( ret == 0 )
+        rsa_copy( rsa, pk.data );
+    else
+        rsa_free( rsa );
+
+    pk_free( &pk );
+
+    return( ret );
 }
 
 /*
@@ -2151,12 +2161,22 @@
  */
 int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
 {
+    int ret;
     pk_context pk;
 
     pk_init( &pk );
-    pk_wrap_rsa( &pk, rsa );
+    pk_set_type( &pk, POLARSSL_PK_RSA );
 
-    return( x509parse_public_keyfile( &pk, path ) );
+    ret = x509parse_public_keyfile( &pk, path );
+
+    if( ret == 0 )
+        rsa_copy( rsa, pk.data );
+    else
+        rsa_free( rsa );
+
+    pk_free( &pk );
+
+    return( ret );
 }
 #endif /* POLARSSL_RSA_C */
 #endif /* POLARSSL_FS_IO */
@@ -2745,12 +2765,22 @@
                        const unsigned char *key, size_t keylen,
                        const unsigned char *pwd, size_t pwdlen )
 {
+    int ret;
     pk_context pk;
 
     pk_init( &pk );
-    pk_wrap_rsa( &pk, rsa );
+    pk_set_type( &pk, POLARSSL_PK_RSA );
 
-    return( x509parse_key( &pk, key, keylen, pwd, pwdlen ) );
+    ret = x509parse_key( &pk, key, keylen, pwd, pwdlen );
+
+    if( ret == 0 )
+        rsa_copy( rsa, pk.data );
+    else
+        rsa_free( rsa );
+
+    pk_free( &pk );
+
+    return( ret );
 }
 
 /*
@@ -2759,12 +2789,22 @@
 int x509parse_public_key_rsa( rsa_context *rsa,
                               const unsigned char *key, size_t keylen )
 {
+    int ret;
     pk_context pk;
 
     pk_init( &pk );
-    pk_wrap_rsa( &pk, rsa );
+    pk_set_type( &pk, POLARSSL_PK_RSA );
 
-    return( x509parse_public_key( &pk, key, keylen ) );
+    ret = x509parse_public_key( &pk, key, keylen );
+
+    if( ret == 0 )
+        rsa_copy( rsa, pk.data );
+    else
+        rsa_free( rsa );
+
+    pk_free( &pk );
+
+    return( ret );
 }
 #endif /* POLARSSL_RSA_C */