2022-01-11 13:46:05 +01:00
|
|
|
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package sop;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
import sop.util.HexUtil;
|
|
|
|
|
|
|
|
public class SessionKey {
|
|
|
|
|
2022-11-06 20:23:01 +01:00
|
|
|
private static final Pattern PATTERN = Pattern.compile("^(\\d):([0-9A-F]+)$");
|
2022-01-11 13:46:05 +01:00
|
|
|
|
|
|
|
private final byte algorithm;
|
|
|
|
private final byte[] sessionKey;
|
|
|
|
|
|
|
|
public SessionKey(byte algorithm, byte[] sessionKey) {
|
|
|
|
this.algorithm = algorithm;
|
|
|
|
this.sessionKey = sessionKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the symmetric algorithm octet.
|
|
|
|
*
|
|
|
|
* @return algorithm id
|
|
|
|
*/
|
|
|
|
public byte getAlgorithm() {
|
|
|
|
return algorithm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the session key.
|
|
|
|
*
|
|
|
|
* @return session key
|
|
|
|
*/
|
|
|
|
public byte[] getKey() {
|
|
|
|
return sessionKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return getAlgorithm() * 17 + Arrays.hashCode(getKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object other) {
|
|
|
|
if (other == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this == other) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!(other instanceof SessionKey)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SessionKey otherKey = (SessionKey) other;
|
|
|
|
return getAlgorithm() == otherKey.getAlgorithm() && Arrays.equals(getKey(), otherKey.getKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SessionKey fromString(String string) {
|
2022-11-06 20:23:01 +01:00
|
|
|
string = string.trim().toUpperCase().replace("\n", "");
|
2022-01-11 13:46:05 +01:00
|
|
|
Matcher matcher = PATTERN.matcher(string);
|
|
|
|
if (!matcher.matches()) {
|
|
|
|
throw new IllegalArgumentException("Provided session key does not match expected format.");
|
|
|
|
}
|
|
|
|
byte algorithm = Byte.parseByte(matcher.group(1));
|
|
|
|
String key = matcher.group(2);
|
|
|
|
|
|
|
|
return new SessionKey(algorithm, HexUtil.hexToBytes(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2022-11-07 16:21:01 +01:00
|
|
|
return Integer.toString(getAlgorithm()) + ':' + HexUtil.bytesToHex(sessionKey);
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
}
|