
org.spongycastle.crypto.tls.DefaultTlsAgreementCredentials Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle
intended for the Android platform. Android unfortunately ships with a stripped-down version of
Bouncy Castle, which prevents easy upgrades - Spongy Castle overcomes this and provides a full,
up-to-date version of the Bouncy Castle cryptographic libs.
package org.spongycastle.crypto.tls;
import java.math.BigInteger;
import org.spongycastle.crypto.BasicAgreement;
import org.spongycastle.crypto.agreement.DHBasicAgreement;
import org.spongycastle.crypto.agreement.ECDHBasicAgreement;
import org.spongycastle.crypto.params.AsymmetricKeyParameter;
import org.spongycastle.crypto.params.DHPrivateKeyParameters;
import org.spongycastle.crypto.params.ECPrivateKeyParameters;
import org.spongycastle.util.BigIntegers;
public class DefaultTlsAgreementCredentials
extends AbstractTlsAgreementCredentials
{
protected Certificate certificate;
protected AsymmetricKeyParameter privateKey;
protected BasicAgreement basicAgreement;
protected boolean truncateAgreement;
public DefaultTlsAgreementCredentials(Certificate certificate, AsymmetricKeyParameter privateKey)
{
if (certificate == null)
{
throw new IllegalArgumentException("'certificate' cannot be null");
}
if (certificate.isEmpty())
{
throw new IllegalArgumentException("'certificate' cannot be empty");
}
if (privateKey == null)
{
throw new IllegalArgumentException("'privateKey' cannot be null");
}
if (!privateKey.isPrivate())
{
throw new IllegalArgumentException("'privateKey' must be private");
}
if (privateKey instanceof DHPrivateKeyParameters)
{
basicAgreement = new DHBasicAgreement();
truncateAgreement = true;
}
else if (privateKey instanceof ECPrivateKeyParameters)
{
basicAgreement = new ECDHBasicAgreement();
truncateAgreement = false;
}
else
{
throw new IllegalArgumentException("'privateKey' type not supported: "
+ privateKey.getClass().getName());
}
this.certificate = certificate;
this.privateKey = privateKey;
}
public Certificate getCertificate()
{
return certificate;
}
public byte[] generateAgreement(AsymmetricKeyParameter peerPublicKey)
{
basicAgreement.init(privateKey);
BigInteger agreementValue = basicAgreement.calculateAgreement(peerPublicKey);
if (truncateAgreement)
{
return BigIntegers.asUnsignedByteArray(agreementValue);
}
return BigIntegers.asUnsignedByteArray(basicAgreement.getFieldSize(), agreementValue);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy