Skip to content

[AI] Fix GOST key blob computation NullPointerException (fixes #94) #227

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 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public class GOSTClientComputations extends KeyExchangeComputations {
public GOSTClientComputations() {}

public void setClientPublicKey(Point point) {
this.clientPublicKeyX =
ModifiableVariableFactory.safelySetValue(
this.clientPublicKeyX, point.getFieldX().getData());
this.clientPublicKeyY =
ModifiableVariableFactory.safelySetValue(
this.clientPublicKeyY, point.getFieldY().getData());
if (point != null && point.getFieldX() != null && point.getFieldY() != null) {
this.clientPublicKeyX =
ModifiableVariableFactory.safelySetValue(
this.clientPublicKeyX, point.getFieldX().getData());
this.clientPublicKeyY =
ModifiableVariableFactory.safelySetValue(
this.clientPublicKeyY, point.getFieldY().getData());
}
}

public ModifiableBigInteger getClientPublicKeyX() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -203,6 +204,10 @@ private void prepareEphemeralKey() {
msg.getComputations().setPrivateKey(chooser.getClientEphemeralEcPrivateKey());
Point publicKey =
curve.mult(msg.getComputations().getPrivateKey().getValue(), curve.getBasePoint());
if (publicKey == null) {
LOGGER.warn("Failed to generate ephemeral public key - using base point");
publicKey = curve.getBasePoint();
}
msg.getComputations().setClientPublicKey(publicKey);
}

Expand Down Expand Up @@ -281,15 +286,38 @@ private void prepareEncryptionParams() {

private void prepareKeyBlob() throws IOException {
try {
if (msg.getComputations().getClientPublicKeyX() == null
|| msg.getComputations().getClientPublicKeyY() == null
|| msg.getComputations().getClientPublicKeyX().getValue() == null
|| msg.getComputations().getClientPublicKeyY().getValue() == null) {
LOGGER.warn(
"Client public key coordinates are not properly initialized - cannot create GOST key blob");
msg.setKeyTransportBlob(new byte[0]);
return;
}

Point ecPoint =
Point.createPoint(
msg.getComputations().getClientPublicKeyX().getValue(),
msg.getComputations().getClientPublicKeyY().getValue(),
chooser.getSelectedGostCurve().getGroupParameters());

if (ecPoint == null) {
LOGGER.warn("Failed to create EC point from coordinates");
msg.setKeyTransportBlob(new byte[0]);
return;
}

PublicKey generatedKey =
GOSTUtils.generatePublicKey(chooser.getSelectedGostCurve(), ecPoint);
if (generatedKey == null) {
LOGGER.warn("Failed to generate public key from EC point");
msg.setKeyTransportBlob(new byte[0]);
return;
}

SubjectPublicKeyInfo ephemeralKey =
SubjectPublicKeyInfo.getInstance(
GOSTUtils.generatePublicKey(chooser.getSelectedGostCurve(), ecPoint)
.getEncoded());
SubjectPublicKeyInfo.getInstance(generatedKey.getEncoded());

Gost2814789EncryptedKey encryptedKey =
new Gost2814789EncryptedKey(
Expand All @@ -309,7 +337,7 @@ private void prepareKeyBlob() throws IOException {
LOGGER.debug("GOST key blob: {}", ASN1Dump.dumpAsString(blob, true));
} catch (Exception e) {
msg.setKeyTransportBlob(new byte[0]);
LOGGER.warn("Could not compute correct GOST key blob: using byte[0]");
LOGGER.warn("Could not compute correct GOST key blob: using byte[0]", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ public static PublicKey generatePublicKey(GOSTCurve curve, Point point) {
private static PublicKey convertPointToPublicKey(
GOSTCurve curve, Point point, String keyFactoryAlg) {
try {
if (point == null || point.getFieldX() == null || point.getFieldY() == null) {
LOGGER.error(
"Cannot convert null point or point with null coordinates to public key");
return null;
}
if (point.getFieldX().getData() == null || point.getFieldY().getData() == null) {
LOGGER.error("Cannot convert point with null coordinate data to public key");
return null;
}
ECParameterSpec ecParameterSpec = getEcParameterSpec(curve);
ECPoint ecPoint = new ECPoint(point.getFieldX().getData(), point.getFieldY().getData());
ECPublicKeySpec privateKeySpec = new ECPublicKeySpec(ecPoint, ecParameterSpec);
Expand Down