use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.
the class CertificateHelper method createPublicKey.
private static PublicKey createPublicKey(final byte[] key, final String algorithm, final String provider) {
try {
final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(key);
final KeyFactory publicKeyFactory = KeyFactory.getInstance(algorithm, provider);
return publicKeyFactory.generatePublic(publicKeySpec);
} catch (final GeneralSecurityException e) {
throw new EncrypterException(String.format("Security exception creating public key for algorithm \"%s\" by provider \"%s\"", algorithm, provider), e);
}
}
use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.
the class RsaEncrypter method decrypt.
public byte[] decrypt(final byte[] rsaEncrypted) {
if (rsaEncrypted == null) {
throw new IllegalArgumentException("Can not decrypt NULL value");
}
try {
final Cipher cipher = this.getCipher();
cipher.init(Cipher.DECRYPT_MODE, this.getSecretEncryptionKey(Cipher.DECRYPT_MODE));
final byte[] decryptedData = cipher.doFinal(rsaEncrypted);
if (this.checkNullBytesPrepended(decryptedData)) {
return Arrays.copyOfRange(decryptedData, BLOCK_SIZE, decryptedData.length);
} else {
return decryptedData;
}
} catch (final Exception e) {
throw new EncrypterException("Could not decrypt secret", e);
}
}
use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.
the class RsaEncrypter method setPublicKeyStore.
public void setPublicKeyStore(final File publicKeyStoreFile) {
try {
final byte[] keyData = Files.readAllBytes(publicKeyStoreFile.toPath());
final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyData);
this.publicKey = KeyFactory.getInstance(ALG).generatePublic(publicKeySpec);
} catch (final NoSuchAlgorithmException | IOException | InvalidKeySpecException e) {
throw new EncrypterException("Could not set public keystore", e);
}
}
use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.
the class AbstractEncryptionProvider method decrypt.
@Override
public byte[] decrypt(final EncryptedSecret secret, final String keyReference) {
if (secret.getType() != this.getType()) {
throw new EncrypterException("EncryptionProvider for type " + this.getType().name() + " cannot decrypt secrets of type " + secret.getType().name());
}
try {
final Cipher cipher = this.getCipher();
cipher.init(Cipher.DECRYPT_MODE, this.getSecretEncryptionKey(keyReference, Cipher.DECRYPT_MODE), this.getAlgorithmParameterSpec());
final byte[] decryptedData = cipher.doFinal(secret.getSecret());
if (this.checkNullBytesPrepended(decryptedData)) {
return Arrays.copyOfRange(decryptedData, BLOCK_SIZE, decryptedData.length);
} else {
return decryptedData;
}
} catch (final Exception e) {
throw new EncrypterException("Could not decrypt secret with keyReference " + keyReference, e);
}
}
use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.
the class Lls1Connector method connect.
@Override
public DlmsConnection connect(final MessageMetadata messageMetadata, final DlmsDevice device, final DlmsMessageListener dlmsMessageListener) throws OsgpException {
// Make sure neither device or device.getIpAddress() is null.
this.checkDevice(device);
this.checkIpAddress(device);
try {
return this.createConnection(messageMetadata, device, dlmsMessageListener, this.secretManagementService::getKeys);
} catch (final UnknownHostException e) {
LOGGER.warn("The IP address is not found: {}", device.getIpAddress(), e);
// Unknown IP, unrecoverable.
throw new TechnicalException(ComponentType.PROTOCOL_DLMS, "The IP address is not found: " + device.getIpAddress());
} catch (final IOException e) {
throw new ConnectionException(e);
} catch (final EncrypterException e) {
LOGGER.error("decryption of security keys failed for device: {}", device.getDeviceIdentification(), e);
throw new TechnicalException(ComponentType.PROTOCOL_DLMS, "decryption of security keys failed for device: " + device.getDeviceIdentification());
}
}
Aggregations