Get rid of pk_wrap_rsa()
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 */