Skip to content

Commit

Permalink
Remember last used key within KeyConfusionAttackDialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
DolphFlynn committed Jan 7, 2025
1 parent b277a76 commit 5a089d1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ public void onAttackKeyConfusionClicked() {
view.window(),
logging,
attackKeys,
lastSigningKeys,
getJWS()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@
import java.awt.*;
import java.util.List;

import static com.blackberry.jwteditor.view.dialog.operations.LastSigningKeys.Signer.KEY_CONFUSION;

/**
* Attack > HMAC Key Confusion dialog from the Editor tab
*/
public class KeyConfusionAttackDialog extends OperationDialog<JWS> {
private static final JWSAlgorithm[] ALGORITHMS = {JWSAlgorithm.HS256, JWSAlgorithm.HS384, JWSAlgorithm.HS512};

private final LastSigningKeys lastSigningKeys;

private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
Expand All @@ -47,25 +53,30 @@ public KeyConfusionAttackDialog(
Window parent,
Logging logging,
List<Key> signingKeys,
LastSigningKeys lastSigningKeys,
JWS jws) {
super(parent, logging, "key_confusion_attack_dialog_title", jws, "error_title_unable_to_sign");

this.lastSigningKeys = lastSigningKeys;

configureUI(contentPane, buttonOK, buttonCancel);

comboBoxSigningKey.setModel(new DefaultComboBoxModel<>(signingKeys.toArray(Key[]::new)));
int lastUsedKeyIndex = lastSigningKeys.lastKeyFor(KEY_CONFUSION).map(signingKeys::indexOf).orElse(-1);
lastUsedKeyIndex = lastUsedKeyIndex == -1 ? 0 : lastUsedKeyIndex;

// Populate the Signing Algorithm dropdown
comboBoxSigningAlgorithm.setModel(new DefaultComboBoxModel<>(new JWSAlgorithm[] {JWSAlgorithm.HS256, JWSAlgorithm.HS384, JWSAlgorithm.HS512}));
comboBoxSigningKey.setModel(new DefaultComboBoxModel<>(signingKeys.toArray(Key[]::new)));
comboBoxSigningKey.setSelectedIndex(lastUsedKeyIndex);

// Select the first signing key
comboBoxSigningKey.setSelectedIndex(0);
comboBoxSigningAlgorithm.setModel(new DefaultComboBoxModel<>(ALGORITHMS));
}

@Override
JWS performOperation() throws SigningException, PemException, UnsupportedKeyException {
JWKKey selectedKey = (JWKKey) comboBoxSigningKey.getSelectedItem();
JWSAlgorithm selectedAlgorithm = (JWSAlgorithm) comboBoxSigningAlgorithm.getSelectedItem();

lastSigningKeys.recordKeyUse(KEY_CONFUSION, selectedKey);

return Attacks.hmacKeyConfusion(jwt, selectedKey, selectedAlgorithm, checkBoxTrailingNewline.isSelected());
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.blackberry.jwteditor.view.dialog.operations;

import com.blackberry.jwteditor.model.keys.Key;
import com.blackberry.jwteditor.view.dialog.operations.SigningDialog.Mode;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class LastSigningKeys {
private final Map<Mode, Key> lastKeyMap = new HashMap<>();
enum Signer { NORMAL, EMBED_JWK, KEY_CONFUSION }

Optional<Key> lastKeyFor(Mode mode) {
private final Map<Signer, Key> lastKeyMap = new HashMap<>();

Optional<Key> lastKeyFor(Signer mode) {
return Optional.ofNullable(lastKeyMap.get(mode));
}

void recordKeyUse(Mode mode, Key key) {
void recordKeyUse(Signer mode, Key key) {
lastKeyMap.put(mode, key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.blackberry.jwteditor.model.keys.JWKKey;
import com.blackberry.jwteditor.model.keys.Key;
import com.blackberry.jwteditor.operations.Attacks;
import com.blackberry.jwteditor.view.dialog.operations.LastSigningKeys.Signer;
import com.nimbusds.jose.JWSAlgorithm;

import javax.swing.*;
Expand All @@ -40,13 +41,16 @@
public class SigningDialog extends OperationDialog<JWS> {

public enum Mode {
NORMAL("sign_dialog_title"),
EMBED_JWK("embed_jwk_attack_dialog_title");
NORMAL("sign_dialog_title", Signer.NORMAL),
EMBED_JWK("embed_jwk_attack_dialog_title", Signer.EMBED_JWK);

final Signer signer;

private final String titleResourceId;

Mode(String titleResourceId) {
Mode(String titleResourceId, Signer embedJwk) {
this.titleResourceId = titleResourceId;
this.signer = embedJwk;
}
}

Expand Down Expand Up @@ -91,7 +95,7 @@ public SigningDialog(
buttonOK.setEnabled(true);
});

int lastUsedKeyIndex = lastSigningKeys.lastKeyFor(mode).map(signingKeys::indexOf).orElse(-1);
int lastUsedKeyIndex = lastSigningKeys.lastKeyFor(mode.signer).map(signingKeys::indexOf).orElse(-1);
lastUsedKeyIndex = lastUsedKeyIndex == -1 ? 0 : lastUsedKeyIndex;
comboBoxSigningKey.setSelectedIndex(lastUsedKeyIndex);

Expand All @@ -107,7 +111,7 @@ JWS performOperation() throws SigningException, NoSuchFieldException, IllegalAcc
JWKKey selectedKey = (JWKKey) comboBoxSigningKey.getSelectedItem();
JWSAlgorithm selectedAlgorithm = (JWSAlgorithm) comboBoxSigningAlgorithm.getSelectedItem();

lastSigningKeys.recordKeyUse(mode, selectedKey);
lastSigningKeys.recordKeyUse(mode.signer, selectedKey);

// Get the header update mode based on the selected radio button, convert to the associated enum value
SigningUpdateMode signingUpdateMode;
Expand Down

0 comments on commit 5a089d1

Please sign in to comment.