forked from TrueCloudLab/frostfs-sdk-java
[#14] add lombok and refactor exceptions. Provide validator.
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
This commit is contained in:
parent
15cf0893c7
commit
388428af76
87 changed files with 819 additions and 970 deletions
|
@ -3,7 +3,9 @@ package info.frostfs.sdk.tools;
|
|||
import com.google.protobuf.Message;
|
||||
import frostfs.session.Types;
|
||||
import info.frostfs.sdk.constants.CryptoConst;
|
||||
import info.frostfs.sdk.exceptions.ResponseException;
|
||||
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
|
||||
import info.frostfs.sdk.exceptions.ResponseFrostFSException;
|
||||
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
|
||||
import info.frostfs.sdk.mappers.response.ResponseStatusMapper;
|
||||
import info.frostfs.sdk.utils.MessageHelper;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
@ -20,16 +22,15 @@ import java.security.Signature;
|
|||
import java.util.Arrays;
|
||||
|
||||
import static info.frostfs.sdk.KeyExtension.getPublicKeyFromBytes;
|
||||
import static info.frostfs.sdk.constants.CryptoConst.RFC6979_SIGNATURE_SIZE;
|
||||
import static info.frostfs.sdk.constants.ErrorConst.INVALID_RESPONSE;
|
||||
import static info.frostfs.sdk.constants.ErrorConst.WRONG_SIGNATURE_SIZE_TEMPLATE;
|
||||
import static info.frostfs.sdk.constants.FieldConst.*;
|
||||
import static java.util.Objects.isNull;
|
||||
import static org.bouncycastle.crypto.util.DigestFactory.createSHA256;
|
||||
import static org.bouncycastle.util.BigIntegers.fromUnsignedByteArray;
|
||||
|
||||
public class Verifier {
|
||||
public static final String ERROR_WRONG_SIG_SIZE_TEMPLATE = "Wrong signature size. Expected length=%s, actual=%s";
|
||||
public static final String ERROR_INVALID_RESPONSE = "Invalid response";
|
||||
public static final int RFC6979_SIG_SIZE = 64;
|
||||
|
||||
private Verifier() {
|
||||
}
|
||||
|
||||
|
@ -56,28 +57,30 @@ public class Verifier {
|
|||
}
|
||||
|
||||
private static BigInteger[] decodeSignature(byte[] sig) {
|
||||
if (sig.length != RFC6979_SIG_SIZE) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format(ERROR_WRONG_SIG_SIZE_TEMPLATE, RFC6979_SIG_SIZE, sig.length)
|
||||
if (sig.length != RFC6979_SIGNATURE_SIZE) {
|
||||
throw new ValidationFrostFSException(
|
||||
String.format(WRONG_SIGNATURE_SIZE_TEMPLATE, RFC6979_SIGNATURE_SIZE, sig.length)
|
||||
);
|
||||
}
|
||||
|
||||
var rs = new BigInteger[2];
|
||||
|
||||
rs[0] = fromUnsignedByteArray(Arrays.copyOfRange(sig, 0, (RFC6979_SIG_SIZE / 2) - 1));
|
||||
rs[1] = fromUnsignedByteArray(Arrays.copyOfRange(sig, RFC6979_SIG_SIZE / 2, RFC6979_SIG_SIZE - 1));
|
||||
rs[0] = fromUnsignedByteArray(Arrays.copyOfRange(sig, 0, (RFC6979_SIGNATURE_SIZE / 2) - 1));
|
||||
rs[1] = fromUnsignedByteArray(
|
||||
Arrays.copyOfRange(sig, RFC6979_SIGNATURE_SIZE / 2, RFC6979_SIGNATURE_SIZE - 1)
|
||||
);
|
||||
return rs;
|
||||
}
|
||||
|
||||
public static void checkResponse(Message response) {
|
||||
if (!verify(response)) {
|
||||
throw new IllegalArgumentException(ERROR_INVALID_RESPONSE);
|
||||
throw new ResponseFrostFSException(INVALID_RESPONSE);
|
||||
}
|
||||
|
||||
var metaHeader = (Types.ResponseMetaHeader) MessageHelper.getField(response, META_HEADER_FIELD_NAME);
|
||||
var status = ResponseStatusMapper.toModel(metaHeader.getStatus());
|
||||
if (!status.isSuccess()) {
|
||||
throw new ResponseException(status);
|
||||
throw new ResponseFrostFSException(status);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -127,7 +130,7 @@ public class Verifier {
|
|||
signature.update(DigestUtils.sha512(data));
|
||||
return signature.verify(Arrays.copyOfRange(sig, 1, sig.length));
|
||||
} catch (Exception exp) {
|
||||
throw new RuntimeException(exp);
|
||||
throw new ProcessFrostFSException(exp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue