Skip to content

Fix bug in SslContext private key reading fall-back path#12746

Merged
normanmaurer merged 1 commit intonetty:4.1from
chrisvest:4.1-pem-inputstream
Aug 27, 2022
Merged

Fix bug in SslContext private key reading fall-back path#12746
normanmaurer merged 1 commit intonetty:4.1from
chrisvest:4.1-pem-inputstream

Conversation

@chrisvest
Copy link
Copy Markdown
Member

Motivation:
We first try to read private keys with BouncyCastle, and then fall back to our own code if that fails.
It could be the case, that BouncyCastle read some data before it failed, and in that case we need to make sure that our fallback code gets to read the key data from the original stream position - not whatever position caused a failure in BouncyCastle.

Modification:
Use the mark and reset methods of InputStream to reset the key stream position, if BouncyCastle cannot read a key.
If the provided input stream does not already support marking, then we wrap it in a BufferedInputStream which does.
We allow up to 1 MiB of data to be buffered for this purpose.
If the key is bigger than this, and BouncyCastle reads more than this before failing, then resetting will throw an exception so we don't get silent failures.

Result:
Fixed private key fallback code path.
Fixes #12745

Motivation:
We first try to read private keys with BouncyCastle, and then fall back to our own code if that fails.
It could be the case, that BouncyCastle read some data before it failed, and in that case we need to make sure that our fallback code gets to read the key data from the original stream position - not whatever position caused a failure in BouncyCastle.

Modification:
Use the mark and reset methods of InputStream to reset the key stream position, if BouncyCastle cannot read a key.
If the provided input stream does not already support marking, then we wrap it in a BufferedInputStream which does.
We allow up to 1 MiB of data to be buffered for this purpose.
If the key is bigger than this, and BouncyCastle reads more than this before failing, then resetting will throw an exception so we don't get silent failures.

Result:
Fixed private key fallback code path.
Fixes netty#12745
@normanmaurer normanmaurer merged commit 6c81a3f into netty:4.1 Aug 27, 2022
normanmaurer pushed a commit that referenced this pull request Aug 27, 2022
Motivation:
We first try to read private keys with BouncyCastle, and then fall back to our own code if that fails.
It could be the case, that BouncyCastle read some data before it failed, and in that case we need to make sure that our fallback code gets to read the key data from the original stream position - not whatever position caused a failure in BouncyCastle.

Modification:
Use the mark and reset methods of InputStream to reset the key stream position, if BouncyCastle cannot read a key.
If the provided input stream does not already support marking, then we wrap it in a BufferedInputStream which does.
We allow up to 1 MiB of data to be buffered for this purpose.
If the key is bigger than this, and BouncyCastle reads more than this before failing, then resetting will throw an exception so we don't get silent failures.

Result:
Fixed private key fallback code path.
Fixes #12745
@chrisvest chrisvest deleted the 4.1-pem-inputstream branch August 27, 2022 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fallback to PemReader fails when BouncyCastlePemReader encounters an unsupported type

2 participants