Commit b64f9841 authored by calocedre TAC's avatar calocedre TAC
Browse files

move exceptions in a dedicated package

parent ddecd30f
......@@ -45,6 +45,7 @@ import org.bouncycastle.jce.spec.ECPublicKeySpec;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.encoders.Hex;
import fr.inria.clea.lsp.exception.CleaEncryptionException;
import fr.inria.clea.lsp.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
......
......@@ -9,6 +9,8 @@ import javax.validation.constraints.Max;
import org.bouncycastle.util.Arrays;
import fr.inria.clea.lsp.exception.CleaEncodingException;
import fr.inria.clea.lsp.exception.CleaEncryptionException;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
......
......@@ -7,6 +7,8 @@ import java.util.Objects;
import java.util.UUID;
import fr.devnied.bitlib.BytesUtils;
import fr.inria.clea.lsp.exception.CleaCryptoException;
import fr.inria.clea.lsp.exception.CleaEncryptionException;
import lombok.Builder;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
......@@ -32,7 +34,7 @@ public class Location {
* @return the deep link as a String
* @throws CleaEncryptionException
*/
public String newDeepLink() throws CleaEncryptionException {
public String newDeepLink() throws CleaCryptoException {
Instant periodStartTime = Instant.now().truncatedTo(ChronoUnit.HOURS);
return this.newDeepLink(periodStartTime);
}
......@@ -46,7 +48,7 @@ public class Location {
* @return the deep link as a String
* @throws CleaEncryptionException
*/
public String newDeepLink(Instant periodStartTime) throws CleaEncryptionException {
public String newDeepLink(Instant periodStartTime) throws CleaCryptoException {
// QR-code validity starts at period start time
return this.newDeepLink(periodStartTime, periodStartTime);
}
......@@ -61,7 +63,7 @@ public class Location {
* @return the deep link as a String
* @throws CleaEncryptionException
*/
public String newDeepLink(Instant periodStartTime, Instant qrCodeValidityStartTime) throws CleaEncryptionException {
public String newDeepLink(Instant periodStartTime, Instant qrCodeValidityStartTime) throws CleaCryptoException {
this.setPeriodStartTime(periodStartTime);
this.setQrCodeValidityStartTime(periodStartTime, qrCodeValidityStartTime);
return COUNTRY_SPECIFIC_PREFIX + this.getLocationSpecificPartEncryptedBase64();
......@@ -72,18 +74,18 @@ public class Location {
* @return the base 64 encoded location specific part
* @throws CleaEncryptionException
*/
public String getLocationSpecificPartEncryptedBase64() throws CleaEncryptionException {
public String getLocationSpecificPartEncryptedBase64() throws CleaCryptoException {
return Base64.getEncoder().encodeToString(this.getLocationSpecificPartEncrypted());
}
protected byte[] getLocationSpecificPartEncrypted() throws CleaEncryptionException {
protected byte[] getLocationSpecificPartEncrypted() throws CleaCryptoException {
if (Objects.nonNull(this.contact)) {
this.locationSpecificPart.setEncryptedLocationContactMessage(this.getLocationContactMessageEncrypted());
}
return new LocationSpecificPartEncoder(this.serverAuthorityPublicKey).encode(locationSpecificPart);
}
protected byte[] getLocationContactMessageEncrypted() throws CleaEncryptionException {
protected byte[] getLocationContactMessageEncrypted() throws CleaCryptoException {
return new LocationContactMessageEncoder(this.manualContactTracingAuthorityPublicKey).encode(contact);
}
......
......@@ -9,6 +9,8 @@ import java.security.spec.InvalidKeySpecException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import fr.devnied.bitlib.BitUtils;
import fr.inria.clea.lsp.exception.CleaCryptoException;
import fr.inria.clea.lsp.exception.CleaEncryptionException;
import fr.inria.clea.lsp.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
......@@ -23,7 +25,7 @@ public class LocationContactMessageEncoder {
cleaEncoder = new CleaEciesEncoder();
}
public byte[] encode(LocationContact message) throws CleaEncryptionException {
public byte[] encode(LocationContact message) throws CleaCryptoException {
try {
byte[] messageBinary = this.getBinaryMessage(message);
byte[] encryptedLocationContactMessage = cleaEncoder.encrypt(null, messageBinary, manualContactTracingAuthorityPublicKey);
......@@ -79,7 +81,7 @@ public class LocationContactMessageEncoder {
* | locationPhone | locationPIN | t_periodStart |
* @throws CleaEncryptionException
*/
public LocationContact decode(byte[] encryptedLocationContactMessage) throws CleaEncryptionException {
public LocationContact decode(byte[] encryptedLocationContactMessage) throws CleaCryptoException {
try {
/* Decrypt the data */
byte[] binaryLocationContactMessage = cleaEncoder.decrypt(encryptedLocationContactMessage, this.manualContactTracingAuthorityPublicKey , false);
......
......@@ -15,6 +15,8 @@ import org.bouncycastle.crypto.InvalidCipherTextException;
import fr.devnied.bitlib.BitUtils;
import fr.inria.clea.lsp.EncryptedLocationSpecificPart.EncryptedLocationSpecificPartBuilder;
import fr.inria.clea.lsp.LocationSpecificPart.LocationSpecificPartBuilder;
import fr.inria.clea.lsp.exception.CleaEncodingException;
import fr.inria.clea.lsp.exception.CleaEncryptionException;
import fr.inria.clea.lsp.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
......@@ -151,7 +153,7 @@ public class LocationSpecificPartDecoder {
.venueCategory2(message.getNextInteger(4))
.periodDuration(message.getNextInteger(8))
.compressedPeriodStartTime(message.getNextInteger(24))
.qrCodeValidityStartTime(TimeUtils.instantFromTimestamp(message.getNextInteger(32)))
.qrCodeValidityStartTime(TimeUtils.instantFromTimestamp(message.getNextLong(32)))
.locationTemporarySecretKey(message.getNextByte(256))
.encryptedLocationContactMessage(encryptedLocationContactMessage);
return locationSpecificPartbuilder.build();
......
......@@ -8,6 +8,7 @@ import java.util.Objects;
import java.util.UUID;
import fr.devnied.bitlib.BitUtils;
import fr.inria.clea.lsp.exception.CleaEncryptionException;
import fr.inria.clea.lsp.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
......
......@@ -4,6 +4,7 @@ import java.time.Instant;
import java.time.temporal.ChronoUnit;
import fr.inria.clea.lsp.Location.LocationBuilder;
import fr.inria.clea.lsp.exception.CleaCryptoException;
import fr.inria.clea.lsp.utils.TimeUtils;
public class LspEncoderDecoder {
......@@ -30,7 +31,7 @@ public class LspEncoderDecoder {
}
}
protected static void decodeLsp(String[] args) throws CleaEncryptionException, CleaEncodingException {
protected static void decodeLsp(String[] args) throws CleaCryptoException {
String lspBase64 = args[1];
String serverAuthoritySecretKey = args[2];
String manualContactTracingAuthoritySecretKey = args[3];
......@@ -49,7 +50,7 @@ public class LspEncoderDecoder {
System.out.println(valuesToreturn);
}
protected static void encodeLsp(String[] args) throws CleaEncryptionException {
protected static void encodeLsp(String[] args) throws CleaCryptoException {
int staff = Integer.parseInt(args[1]);
int countryCode = Integer.parseInt(args[2]);
int qrCodeRenewalIntervalExponentCompact = Integer.parseInt(args[3]);
......
package fr.inria.clea.lsp.exception;
public abstract class CleaCryptoException extends Exception {
private static final long serialVersionUID = 1L;
public CleaCryptoException() {
super();
}
public CleaCryptoException(String message) {
super(message);
}
public CleaCryptoException(Throwable cause) {
super(cause);
}
}
package fr.inria.clea.lsp;
package fr.inria.clea.lsp.exception;
/**
* Generic Clea exception thrown when the layout of the message
* is not respected.
*/
public class CleaEncodingException extends Exception {
public class CleaEncodingException extends CleaCryptoException {
private static final long serialVersionUID = 1L;
public CleaEncodingException(String message) {
......
package fr.inria.clea.lsp;
package fr.inria.clea.lsp.exception;
/**
* Generic Clea exception thrown when something went wrong while encoding / decoding.
*/
public class CleaEncryptionException extends Exception {
public class CleaEncryptionException extends CleaCryptoException {
private static final long serialVersionUID = 1L;
public CleaEncryptionException(Throwable cause) {
......
......@@ -33,6 +33,9 @@ import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import fr.devnied.bitlib.BytesUtils;
import fr.inria.clea.lsp.exception.CleaCryptoException;
import fr.inria.clea.lsp.exception.CleaEncodingException;
import fr.inria.clea.lsp.exception.CleaEncryptionException;
import fr.inria.clea.lsp.utils.TimeUtils;
/**
......@@ -79,7 +82,7 @@ class LocationSpecificPartTest {
}
@Test
public void testEncodinsAndDecodingOfALocationMessage() throws CleaEncryptionException {
public void testEncodinsAndDecodingOfALocationMessage() throws CleaCryptoException {
Instant periodStartTime = Instant.now().truncatedTo(ChronoUnit.HOURS);
LocationContact locationContact = new LocationContact("0612150292", "01234567", periodStartTime);
Location location = Location.builder().contact(locationContact)
......@@ -94,7 +97,7 @@ class LocationSpecificPartTest {
}
@Test
public void testEncodingAndDecodingOfALocationSpecificPart() throws CleaEncryptionException, CleaEncodingException {
public void testEncodingAndDecodingOfALocationSpecificPart() throws CleaCryptoException {
Instant periodStartTime = Instant.now().truncatedTo(ChronoUnit.HOURS);
LocationContact locationContact = new LocationContact("33800130000", "01234567", periodStartTime);
/* Encode a LSP with location */
......@@ -106,6 +109,7 @@ class LocationSpecificPartTest {
.serverAuthorityPublicKey(serverAuthorityKeyPair[1])
.permanentLocationSecretKey(permanentLocationSecretKey).build();
location.setPeriodStartTime(periodStartTime);
location.setQrCodeValidityStartTime(periodStartTime, periodStartTime);
/* Encode a LSP with location */
String encryptedLocationSpecificPart = location.getLocationSpecificPartEncryptedBase64();
......@@ -120,7 +124,7 @@ class LocationSpecificPartTest {
@Test
public void testEncodingAndDecodingOfALocationSpecificPartWithoutLocationContact()
throws CleaEncryptionException, CleaEncodingException {
throws CleaCryptoException {
Instant periodStartTime = Instant.now().truncatedTo(ChronoUnit.HOURS);
/* Encode a LSP with location */
LocationSpecificPart lsp = LocationSpecificPart.builder().staff(true).countryCode(33)
......@@ -226,14 +230,14 @@ class LocationSpecificPartTest {
public void testEncodingDecodingOfLSPSpecificPartInBase64(int staff, int countryCode,
String locationTemporaryPublicID, int qrCodeRenewalIntervalExponentCompact, int venueType, int venueCat1,
int venueCat2, int periodDuration, int periodStartTime, long qrStartTime, String serverAuthoritySecretKey,
String serverAuthorityPublicKey, String lspbase64) throws CleaEncryptionException, CleaEncodingException {
String serverAuthorityPublicKey, String lspbase64) throws CleaCryptoException {
/* Use only testLSPDecoding.csv parameters to have a variety of parameters */
/* times parameters and location are generated */
Instant myPeriodStartTime = Instant.now().truncatedTo(ChronoUnit.HOURS);
Random rn = new Random();
int nbDigits = rn.nextInt(6) + 10;
String phone = generateRandomDigits(nbDigits);
String pinCode = generateRandomDigits(8);
String pinCode = generateRandomDigits(6);
LocationContact locationContact = new LocationContact(phone, pinCode, myPeriodStartTime);
/* Encode a LSP with location */
LocationSpecificPart lsp = LocationSpecificPart.builder().staff(staff == 1).countryCode(countryCode)
......@@ -264,7 +268,7 @@ class LocationSpecificPartTest {
public void testDecodingOfLocationOnlyInBase64(String locationPhone, String locationPin, long t_periodStart,
String serverAuthoritySecretKey, String serverAuthorityPublicKey,
String manualContactTracingAuthoritySecretKey, String manualContactTracingAuthorityPublicKey,
String lspbase64) throws CleaEncryptionException, CleaEncodingException {
String lspbase64) throws CleaCryptoException {
/* Decode the encoded LSP */
LocationSpecificPartDecoder decoder = new LocationSpecificPartDecoder(serverAuthoritySecretKey);
LocationSpecificPart lsp = decoder.decrypt(lspbase64);
......
......@@ -11,6 +11,8 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import fr.inria.clea.lsp.exception.CleaCryptoException;
// @ExtendWith(MockitoJUnitRunner.class)
public class LocationTest {
......@@ -80,7 +82,7 @@ public class LocationTest {
}
@Test
public void testNewDeepLink() throws CleaEncryptionException {
public void testNewDeepLink() throws CleaCryptoException {
int qrCodeRenewalIntervalExponentCompact = 2;
int periodDuration = 3;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment