8000 Tls injection mechanism by sergejskozlovics · Pull Request #1517 · bcgit/bc-java · GitHub
[go: up one dir, main page]

Skip to content

Tls injection mechanism #1517

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
afe7f4e
Added multiples classes implementing the TLS injection mechanism in BC
sergejskozlovics Oct 25, 2023
33abb91
Added code that makes injected KEMs visible to TLS
sergejskozlovics Oct 25, 2023
006ee29
Re-implemented NamedGroupInfo to support injected KEMs
sergejskozlovics Oct 25, 2023
04d11e6
Re-implemented SignatureSchemeInfo to support injected Sig Algs
sergejskozlovics Oct 25, 2023
2c97329
added injected KEMs to the TLS supported groups extension
sergejskozlovics Oct 25, 2023
77b40de
adding injected KEMs and signature algorithms to the list of supporte…
sergejskozlovics Oct 25, 2023
d9c4416
using KEM as a TlsAgreement
sergejskozlovics Oct 25, 2023
fa0b30b
adding injected KEMs and signature algorithms to JcaTlsCrypto
sergejskozlovics Oct 25, 2023
c433374
Added code that makes signature algorithms (injected via InjectionPoi…
sergejskozlovics Oct 25, 2023
7450b06
Adding support for injected algorithms (via InjectionPoint) to the BC…
sergejskozlovics Oct 25, 2023
ece83e0
moved TLS injection mechanism from BC tls folder to core
sergejskozlovics Oct 27, 2023
e2d73dd
some changes in imports
sergejskozlovics Oct 27, 2023
4861e5e
changed arg ordering
sergejskozlovics Oct 27, 2023
8fdce66
More secure pop() method for InjectionPoint. Better logic for inserti…
sergejskozlovics Nov 23, 2023
8ab2aa2
Merge remote-tracking branch 'upstream/main' into tls-injection2
sergejskozlovics Apr 14, 2024
dc6a801
moved the main TLS Injection Mechanism out from the core package, lea…
sergejskozlovics Apr 19, 2024
48792c7
refactored TLS Injection Mechanism; moved it to the tls package
sergejskozlovics Apr 19, 2024
45dd268
fixed comments
sergejskozlovics Apr 19, 2024
ee644df
Removed UniversalKeyPairGenerator, since it is not used by the TLS In…
sergejskozlovics Apr 19, 2024
57af27a
renamed variables "*EncodedKey" to "*ByteKey"; add "RSASSA-PSS" as a …
sergejskozlovics Jun 3, 2024
9a2e253
added .clone() for the shared key, since BC may alter it
sergejskozlovics Jun 14, 2024
8449c71
added isEmpty() to InjectionPoint
sergejskozlovics Jun 14, 2024
afb1a44
fixed a bug when KEM keyGen() was unnecessarily invoked at the server…
sergejskozlovics Jun 14, 2024
ac2006b
adding the BC provider in InjectionPoint.push
sergejskozlovics Jun 18, 2024
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
Prev Previous commit
Next Next commit
renamed variables "*EncodedKey" to "*ByteKey"; add "RSASSA-PSS" as a …
…fallback algorithm for the BC adapter (since <hash-name>WITHRSAANDMGF1 sometimes is not found)
  • Loading branch information
sergejskozlovics committed Jun 3, 2024
commit 57af27a58c1b7d44cda7a2e06a28c67a6d716c57
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,20 @@ public Tls13Verifier createVerifier(int signatureScheme) throws IOException
int cryptoHashAlgorithm = SignatureScheme.getCryptoHashAlgorithm(signatureScheme);
String digestName = crypto.getDigestName(cryptoHashAlgorithm);
String sigName = org.bouncycastle.tls.crypto.impl.jcajce.RSAUtil.getDigestSigAlgName(digestName)
//+"WITHRSA";//+
+ "WITHRSAANDMGF1";

// NOTE: We explicitly set them even though they should be the defaults, because providers vary
AlgorithmParameterSpec pssSpec = org.bouncycastle.tls.crypto.impl.jcajce.RSAUtil
.getPSSParameterSpec(cryptoHashAlgorithm, digestName, crypto.getHelper());

return crypto.createTls13Verifier(sigName, pssSpec, getPubKeyRSA());
try {
return crypto.createTls13Verifier(sigName, pssSpec, getPubKeyRSA());
}
catch(Exception e) {
// #tls-injection fix: using the sig alg name of the SunRsaSign provider
sigName = "RSASSA-PSS";
return crypto.createTls13Verifier(sigName, pssSpec, getPubKeyRSA());
}
}

// TODO[RFC 8998]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,14 @@ public AlgorithmParameters getSignatureSchemeAlgorithmParameters(int signatureSc

AlgorithmParameterSpec pssSpec = RSAUtil.getPSSParameterSpec(cryptoHashAlgorithm, digestName, getHelper());

Signature signer = getHelper().createSignature(sigName);
Signature signer;
try {
signer = getHelper().createSignature(sigName);
}
catch(Exception e) {
signer = Signature.getInstance("RSASSA-PSS", "SunRsaSign");
// #tls-injection fix: using the sig alg name of the SunRsaSign provider
}

// NOTE: We explicitly set them even though they should be the defaults, because providers vary
signer.setParameter(pssSpec);
Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ public TlsStreamSigner getStreamSigner(SignatureAndHashAlgorithm algorithm) thro
AlgorithmParameterSpec pssSpec = RSAUtil.getPSSParameterSpec(cryptoHashAlgorithm, digestName,
crypto.getHelper());

return crypto.createStreamSigner(sigName, pssSpec, privateKey, true);
try {
return crypto.createStreamSigner(sigName, pssSpec, privateKey, true);
}
catch(Exception e) {
// #tls-injection fix: using the sig alg name of the SunRsaSign provider
sigName = "RSASSA-PSS";
return crypto.createStreamSigner(sigName, pssSpec, privateKey, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.bouncycastle.crypto.CipherParameters;

public interface CipherParametersToEncodedKey
public interface CipherParametersByteKey
{
byte[] encodedKey(CipherParameters params);
byte[] byteKey(CipherParameters params);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ boolean verifySignature(
}

private final Map<Integer, VerifySignatureFunction> verifiers; // code point -> verifier fn
private final Map<Integer, PublicKeyToEncodedKey> converters; // code point -> encoder fn
private final Map<Integer, PublicKeyToByteKey> converters; // code point -> encoder fn

public InjectedSigVerifiers()
{
Expand All @@ -49,7 +49,7 @@ public InjectedSigVerifiers(InjectedSigVerifiers origin)
public void add(
int sigSchemeCodePoint,
VerifySignatureFunction fn,
PublicKeyToEncodedKey fn2)
PublicKeyToByteKey fn2)
{
verifiers.put(sigSchemeCodePoint, fn);
converters.put(sigSchemeCodePoint, fn2);
Expand All @@ -66,7 +66,7 @@ public TlsVerifier tlsVerifier(
int sigSchemeCodePoint)
{
VerifySignatureFunction fn = verifiers.get(sigSchemeCodePoint);
PublicKeyToEncodedKey fn2 = converters.get(sigSchemeCodePoint);
PublicKeyToByteKey fn2 = converters.get(sigSchemeCodePoint);

return new MyTlsVerifier(crypto, publicKey, sigSchemeCodePoint, fn, fn2);
}
Expand All @@ -79,14 +79,14 @@ private class MyTlsVerifier
private final PublicKey publicKey;
private final int signatureScheme;
private final VerifySignatureFunction fn;
private final PublicKeyToEncodedKey fn2;
private final PublicKeyToByteKey fn2;

public MyTlsVerifier(
JcaTlsCrypto crypto,
PublicKey publicKey,
int signatureSchemeCodePoint,
VerifySignatureFunction fn,
PublicKeyToEncodedKey fn2)
PublicKeyToByteKey fn2)
{
if (null == crypto)
{
Expand All @@ -112,7 +112,7 @@ public boolean verifyRawSignature(
DigitallySigned signature,
byte[] hash) throws IOException
{
byte[] encoded = fn2.encodedKey(publicKey);
byte[] encoded = fn2.byteKey(publicKey);
boolean b = fn.verifySignature(hash, encoded, signature);
return b;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,10 @@ public TlsSigner tlsSigner(
throw new RuntimeException("Algorithm " + algorithmFullName + " not found among signers.");
}

byte[] sk = privateKey.getEncoded();
PrivateKeyInfo info = PrivateKeyInfo.getInstance(sk);
byte[] skEncoded = privateKey.getEncoded();
PrivateKeyInfo info = PrivateKeyInfo.getInstance(skEncoded);

byte[] sk2;
try
{
sk2 = info.getPrivateKey().getEncoded();
} catch (IOException e)
{
throw new RuntimeException(e);
}
return new MyTlsSigner(crypto, sk2, (SignerFunction) fn);
byte[] skBytes = info.getPrivateKey().getOctets();
return new MyTlsSigner(crypto, skBytes, (SignerFunction) fn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class MyMessag 93D4 eSigner
private SignatureAndHashAlgorithm algorithm;
private SignerFunction fnSign;
private VerifierFunction fnVerify;
private CipherParametersToEncodedKey paramsToPublicKey, paramsToPrivateKey;
private CipherParametersByteKey paramsToPublicKey, paramsToPrivateKey;

// the following fields are initialized by BC by invoking init():
private CipherParameters params;
Expand All @@ -21,8 +21,8 @@ public MyMessageSigner(
int signatureSchemeCodePoint,
SignerFunction fnSign,
VerifierFunction fnVerify,
CipherParametersToEncodedKey paramsToPublicKey,
CipherParametersToEncodedKey paramsToPrivateKey)
CipherParametersByteKey paramsToPublicKey,
CipherParametersByteKey paramsToPrivateKey)
{
this.algorithm = new SignatureAndHashAlgorithm((short) (signatureSchemeCodePoint >> 8), (short) (signatureSchemeCodePoint & 0xFF));
this.fnSign = fnSign;
Expand All @@ -43,7 +43,7 @@ public void init(
@Override
public byte[] generateSignature(byte[] message)
{
byte[] sk = this.paramsToPrivateKey.encodedKey(params); //skParams.getEncoded();
byte[] sk = this.paramsToPrivateKey.byteKey(params); //skParams.getEncoded();

byte[] bcSignature = new byte[0];
try
Expand All @@ -61,7 +61,8 @@ public boolean verifySignature(
byte[] message,
byte[] signature)
{
byte[] pk = this.paramsToPublicKey.encodedKey(params);
return fnVerify.verify(message, pk, new DigitallySigned(algorithm, signature));
byte[] pk = this.paramsToPublicKey.byteKey(params);
boolean isValid = fnVerify.verify(message, pk, new DigitallySigned(algorithm, signature));
return isValid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.security.PublicKey;

public interface PublicKeyToEncodedKey
public interface PublicKeyToByteKey
{
byte[] encodedKey(PublicKey key);
byte[] byteKey(PublicKey key);
}
0