@@ -687,29 +687,6 @@ int wp_encrypt_key(WOLFPROV_CTX* provCtx, const char* cipherName,
687687#endif
688688}
689689
690- /* TODO: Structure could change! */
691- /*
692- * Copy of Core BIO structure as it isn't public and need to get the BIO out.
693- */
694-
695- #if OPENSSL_VERSION_PREREQ (3 ,2 )
696- struct ossl_core_bio_st {
697- /* Reference count. */
698- int ref_cnt ;
699- /* Underlying BIO. */
700- BIO * bio ;
701- };
702- #else
703- struct ossl_core_bio_st {
704- /* Reference count. */
705- int ref_cnt ;
706- /* Read/write reference count lock. */
707- CRYPTO_RWLOCK * ref_lock ;
708- /* Underlying BIO. */
709- BIO * bio ;
710- };
711- #endif
712-
713690/**
714691 * Read data out of the core BIO.
715692 *
@@ -719,22 +696,29 @@ struct ossl_core_bio_st {
719696 * @return 1 on success.
720697 * @return 0 on failure.
721698 */
722- int wp_read_der_bio (OSSL_CORE_BIO * coreBio , unsigned char * * data , word32 * len )
699+ int wp_read_der_bio (WOLFPROV_CTX * provctx , OSSL_CORE_BIO * coreBio , unsigned char * * data , word32 * len )
723700{
724701 int ok = 1 ;
725- long readLen ;
702+ long readLen = 1 ;
726703 unsigned char buf [128 ]; /* Read 128 bytes at a time. */
727704 unsigned char * p ;
728705
729- do {
730- readLen = BIO_read (coreBio -> bio , buf , sizeof (buf ));
706+ BIO * bio = wp_corebio_get_bio (provctx , coreBio );
707+ if (bio == NULL ) {
708+ ok = 0 ;
709+ }
710+
711+ while (ok && (readLen > 0 )) {
712+ readLen = BIO_read (bio , buf , sizeof (buf ));
731713 if (readLen < -1 ) {
714+ WOLFPROV_MSG (WP_LOG_PROVIDER , "BIO_read error (%d) in %s:%d" , readLen , __FILE__ , __LINE__ );
732715 ok = 0 ;
733716 }
734717 if (ok && (readLen > 0 )) {
735718 /* Reallocate for new data. */
736719 p = OPENSSL_realloc (* data , * len + readLen );
737720 if (p == NULL ) {
721+ WOLFPROV_MSG (WP_LOG_PROVIDER , "OPENSSL_realloc error (%d) in %s:%d" , readLen , __FILE__ , __LINE__ );
738722 ok = 0 ;
739723 }
740724 }
@@ -745,22 +729,42 @@ int wp_read_der_bio(OSSL_CORE_BIO *coreBio, unsigned char** data, word32* len)
745729 * len += readLen ;
746730 }
747731 }
748- while (ok && (readLen > 0 ));
749732
733+ BIO_free (bio );
750734 WOLFPROV_LEAVE (WP_LOG_PROVIDER , __FILE__ ":" WOLFPROV_STRINGIZE (__LINE__ ), ok );
751735 return ok ;
752736}
753737
738+ static OSSL_FUNC_BIO_up_ref_fn * c_bio_up_ref = NULL ;
739+ static int wolfssl_prov_bio_up_ref (OSSL_CORE_BIO * bio )
740+ {
741+ if (c_bio_up_ref == NULL )
742+ return 0 ;
743+ return c_bio_up_ref (bio );
744+ }
745+
754746/**
755- * Get the underlying BIO from the core BIO.
747+ * Get the underlying BIO object from the core BIO.
756748 *
757749 * @param [in] coreBio Core BIO.
758750 * @return NULL on failure.
759751 * @return Underlying BIO on success.
760752 */
761- BIO * wp_corebio_get_bio (OSSL_CORE_BIO * coreBio )
753+ BIO * wp_corebio_get_bio (WOLFPROV_CTX * provCtx , OSSL_CORE_BIO * coreBio )
762754{
763- return coreBio -> bio ;
755+ BIO * outBio ;
756+
757+ if ((provCtx == NULL ) || (provCtx -> coreBioMethod == NULL ))
758+ return NULL ;
759+
760+ if ((outBio = BIO_new (provCtx -> coreBioMethod )) == NULL )
761+ return NULL ;
762+ if (!wolfssl_prov_bio_up_ref (coreBio )) {
763+ BIO_free (outBio );
764+ return NULL ;
765+ }
766+ BIO_set_data (outBio , coreBio );
767+ return outBio ;
764768}
765769
766770
0 commit comments