Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add support for multi item PEM parsing #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/wp_dec_pem2der.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ static word32 wp_pem2der_find_header(unsigned char* data, word32 len)
return idx;
}

/**
* Find the start of the PEM footer.
*
* @param [in] data Data buffer with PEM encoding.
* @param [in] len Length of data in bytes.
* @return Index of PEM header on success.
* @return Length of data on failure.
*/
static word32 wp_pem2der_find_footer(unsigned char* data, word32 len)
{
word32 i;
word32 idx = len;

for (i = 0; i + 8 < len; i++) {
if ((data[i] == '-') && (XMEMCMP(data + i, "-----END", 8) == 0)) {
idx = i;
break;
}
}

return idx;
}

#ifdef WOLFSSL_ENCRYPTED_KEYS
/**
* Password callback data.
Expand Down Expand Up @@ -409,6 +432,17 @@ static int wp_pem2der_decode(wp_Pem2Der* ctx, OSSL_CORE_BIO* coreBio,
dataCbArg, pwCb, pwCbArg);
}
}
if (data != NULL) {
/* Restore BIO position to end of first footer */
idx = wp_pem2der_find_footer(data, len);
BIO *bio = wp_corebio_get_bio(ctx->provCtx, coreBio);
if (BIO_seek(bio, idx) == -1) {
WOLFPROV_ERROR_MSG(WP_LOG_PK, "Error resetting BIO position");
}

BIO_free(bio);
}

/* Dispose of the PEM data buffer. */
OPENSSL_free(data);

Expand Down
Loading