Skip to content

Commit 1afff09

Browse files
author
Andras Fekete
committed
Fix wp_corebio_get_bio
1 parent f77fdbe commit 1afff09

File tree

10 files changed

+171
-44
lines changed

10 files changed

+171
-44
lines changed

include/wolfprovider/internal.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <openssl/core.h>
2222
#include <openssl/core_names.h>
2323
#include <openssl/types.h>
24+
#include <openssl/bio.h>
2425

2526
#ifdef WOLFENGINE_USER_SETTINGS
2627
#include "user_settings.h"
@@ -129,6 +130,7 @@ typedef struct WOLFPROV_CTX {
129130
/** Mutex for use of random number generator. */
130131
wolfSSL_Mutex rng_mutex;
131132
#endif
133+
BIO_METHOD *coreBioMethod;
132134
} WOLFPROV_CTX;
133135

134136

@@ -157,8 +159,8 @@ int wp_encrypt_key(WOLFPROV_CTX* provCtx, const char* cipherName,
157159
unsigned char* keyData, size_t* keyLen, word32 pkcs8Len,
158160
OSSL_PASSPHRASE_CALLBACK *pwCb, void *pwCbArg, byte** cipherInfo);
159161

160-
int wp_read_der_bio(OSSL_CORE_BIO *coreBio, unsigned char** data, word32* len);
161-
BIO* wp_corebio_get_bio(OSSL_CORE_BIO *coreBio);
162+
int wp_read_der_bio(WOLFPROV_CTX* provCtx, OSSL_CORE_BIO *coreBio, unsigned char** data, word32* len);
163+
BIO* wp_corebio_get_bio(WOLFPROV_CTX* provCtx, OSSL_CORE_BIO *coreBio);
162164

163165
byte wp_ct_byte_mask_eq(byte a, byte b);
164166
byte wp_ct_byte_mask_ne(byte a, byte b);

src/wp_dec_epki2pki.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ static int wp_epki2pki_decode(wp_Epki2Pki* ctx, OSSL_CORE_BIO* coreBio,
197197
(void)selection;
198198

199199
/* Read the data from the BIO into buffer that is allocated on the fly. */
200-
if (!wp_read_der_bio(coreBio, &data, &len)) {
200+
if (!wp_read_der_bio(ctx->provCtx, coreBio, &data, &len)) {
201201
ok = 0;
202202
}
203203
/* No data - nothing to do. */

src/wp_dec_pem2der.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ static int wp_pem2der_decode(wp_Pem2Der* ctx, OSSL_CORE_BIO* coreBio,
391391
(void)selection;
392392

393393
/* Read the data from the BIO into buffer that is allocated on the fly. */
394-
if (!wp_read_der_bio(coreBio, &data, &len)) {
394+
if (!wp_read_der_bio(ctx->provCtx, coreBio, &data, &len)) {
395395
ok = 0;
396396
}
397397
/* No data - nothing to do. */

src/wp_dh_kmgmt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,7 +2114,7 @@ static int wp_dh_decode(wp_DhEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
21142114
if (dh == NULL) {
21152115
ok = 0;
21162116
}
2117-
if (ok && (!wp_read_der_bio(cBio, &data, &len))) {
2117+
if (ok && (!wp_read_der_bio(ctx->provCtx, cBio, &data, &len))) {
21182118
ok = 0;
21192119
}
21202120
if (ok && (ctx->format == WP_ENC_FORMAT_TYPE_SPECIFIC)) {
@@ -2421,7 +2421,7 @@ static int wp_dh_encode(wp_DhEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
24212421
int ok = 1;
24222422
#if (LIBWOLFSSL_VERSION_HEX >= 0x05000000 && defined(WOLFSSL_DH_EXTRA))
24232423
int rc;
2424-
BIO* out = wp_corebio_get_bio(cBio);
2424+
BIO* out = wp_corebio_get_bio(ctx->provCtx, cBio);
24252425
unsigned char* keyData = NULL;
24262426
size_t keyLen;
24272427
unsigned char* derData = NULL;

src/wp_ecc_kmgmt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,7 +2048,7 @@ static int wp_ecc_decode(wp_EccEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
20482048
if (ecc == NULL) {
20492049
ok = 0;
20502050
}
2051-
if (ok && (!wp_read_der_bio(cBio, &data, &len))) {
2051+
if (ok && (!wp_read_der_bio(ctx->provCtx, cBio, &data, &len))) {
20522052
ok = 0;
20532053
}
20542054
if (ok && (ctx->format == WP_ENC_FORMAT_TYPE_SPECIFIC)) {
@@ -2393,7 +2393,7 @@ static int wp_ecc_encode(wp_EccEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
23932393
{
23942394
int ok = 1;
23952395
int rc;
2396-
BIO* out = wp_corebio_get_bio(cBio);
2396+
BIO* out = wp_corebio_get_bio(ctx->provCtx, cBio);
23972397
unsigned char* keyData = NULL;
23982398
size_t keyLen;
23992399
unsigned char* derData = NULL;

src/wp_ecx_kmgmt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,7 +1940,7 @@ static int wp_ecx_decode(wp_EcxEncDecCtx* ctx, OSSL_CORE_BIO* cBio,
19401940
}
19411941

19421942
if (ok) {
1943-
ok = wp_read_der_bio(cBio, &data, &len);
1943+
ok = wp_read_der_bio(ctx->provCtx, cBio, &data, &len);
19441944
}
19451945
if (ok) {
19461946
rc = ctx->decode(data, &idx, (void*)&ecx->key, len);
@@ -1992,7 +1992,7 @@ static int wp_ecx_encode(wp_EcxEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
19921992
{
19931993
int ok = 1;
19941994
int rc;
1995-
BIO* out = wp_corebio_get_bio(cBio);
1995+
BIO* out = wp_corebio_get_bio(ctx->provCtx, cBio);
19961996
unsigned char* keyData = NULL;
19971997
size_t keyLen = 0;
19981998
unsigned char derData[160];

src/wp_file_store.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ static wp_FileCtx* wp_file_open(WOLFPROV_CTX* provCtx, const char* uri)
115115
ok = 0;
116116
}
117117
if (ok) {
118+
if (ctx->bio != NULL) {
119+
BIO_free(ctx->bio);
120+
}
118121
/* Create a BIO to access file. */
119122
ctx->bio = BIO_new_file(uri, "rb");
120123
if (ctx->bio == NULL) {
@@ -145,8 +148,11 @@ static wp_FileCtx* wp_file_attach(WOLFPROV_CTX* provCtx, OSSL_CORE_BIO* cBio)
145148

146149
ctx = wp_filectx_new(provCtx);
147150
if (ctx != NULL) {
151+
if (ctx->bio != NULL) {
152+
BIO_free(ctx->bio);
153+
}
148154
/* Get the internal BIO. */
149-
ctx->bio = wp_corebio_get_bio(cBio);
155+
ctx->bio = wp_corebio_get_bio(provCtx, cBio);
150156
}
151157

152158
return ctx;

src/wp_internal.c

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/wp_rsa_kmgmt.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,7 @@ static int wp_rsa_decode(wp_RsaEncDecCtx* ctx, OSSL_CORE_BIO* cBio,
19361936
ok = 0;
19371937
}
19381938
if (ok) {
1939-
ok = wp_read_der_bio(cBio, &data, &len);
1939+
ok = wp_read_der_bio(ctx->provCtx, cBio, &data, &len);
19401940
}
19411941
if (ok && (ctx->format == WP_ENC_FORMAT_SPKI)) {
19421942
if (!wp_rsa_decode_spki(rsa, data, len)) {
@@ -2333,7 +2333,7 @@ static int wp_rsa_encode(wp_RsaEncDecCtx* ctx, OSSL_CORE_BIO* cBio,
23332333
{
23342334
int ok = 1;
23352335
int rc;
2336-
BIO* out = wp_corebio_get_bio(cBio);
2336+
BIO *out = wp_corebio_get_bio(ctx->provCtx, cBio);
23372337
unsigned char* keyData = NULL;
23382338
size_t keyLen;
23392339
unsigned char* derData = NULL;
@@ -2468,6 +2468,7 @@ static int wp_rsa_encode(wp_RsaEncDecCtx* ctx, OSSL_CORE_BIO* cBio,
24682468
OPENSSL_free(derData);
24692469
OPENSSL_free(pemData);
24702470
}
2471+
BIO_free(out);
24712472
WOLFPROV_LEAVE(WP_LOG_PK, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
24722473
return ok;
24732474
}

0 commit comments

Comments
 (0)