use of com.fathomdb.crypto.CryptoKey in project platformlayer by platformlayer.
the class SecretStore method getSecretFromPassword.
public CryptoKey getSecretFromPassword(int userId, final String password) {
SecretStoreDecoder visitor = new SecretStoreDecoder() {
@Override
public void visitPassword(byte[] salt, byte[] data) {
CryptoKey secretKey = FathomdbCrypto.deriveKey(salt, password);
setSecretKey(decryptSymetricKey(secretKey, data));
}
};
try {
read(encoded, visitor);
} catch (IOException e) {
throw new IllegalArgumentException("Secret data is corrupted", e);
}
return visitor.getSecretKey();
}
use of com.fathomdb.crypto.CryptoKey in project platformlayer by platformlayer.
the class JdbcManagedItemRepository method createManagedItem.
@Override
@JdbcTransaction
public <T extends ItemBase> T createManagedItem(ProjectId project, T item) throws RepositoryException {
DbHelper db = new DbHelper(item.getClass(), project);
try {
CryptoKey itemSecret = FathomdbCrypto.generateKey();
byte[] data = serialize(item, itemSecret);
byte[] secretData = itemSecrets.encodeItemSecret(itemSecret);
int itemId = db.insertItem(item, data, secretData);
Tags tags = item.tags;
if (tags != null && !tags.isEmpty()) {
db.insertTags(itemId, tags);
}
return item;
} catch (SQLException e) {
throw new RepositoryException("Error running query", e);
} finally {
db.close();
}
}
use of com.fathomdb.crypto.CryptoKey in project platformlayer by platformlayer.
the class JdbcServiceAuthorizationRepository method setPrivateData.
@Override
@JdbcTransaction
public void setPrivateData(ServiceType serviceType, ProjectId project, ServiceMetadataKey metadataKey, String value) throws RepositoryException {
DbHelper db = new DbHelper(serviceType, project, metadataKey);
try {
CryptoKey secret = FathomdbCrypto.generateKey();
byte[] plaintext = Utf8.getBytes(value);
byte[] ciphertext = FathomdbCrypto.encrypt(secret, plaintext);
// TODO: Encode this differently from items??
byte[] secretData = secretHelper.encodeItemSecret(secret);
if (serviceType == null) {
db.insertProjectMetadata(ciphertext, secretData);
} else {
db.insertServiceMetadata(ciphertext, secretData);
}
} catch (SQLException e) {
throw new RepositoryException("Error running query", e);
} finally {
db.close();
}
}
use of com.fathomdb.crypto.CryptoKey in project platformlayer by platformlayer.
the class JdbcServiceAuthorizationRepository method findPrivateData.
@Override
@JdbcTransaction
public String findPrivateData(ServiceType serviceType, ProjectId project, ServiceMetadataKey metadataKey) throws RepositoryException {
DbHelper db = new DbHelper(serviceType, project, metadataKey);
List<String> values = Lists.newArrayList();
ResultSet rs = null;
try {
if (serviceType == null) {
rs = db.selectProjectMetadata();
} else {
rs = db.selectServiceMetadata();
}
while (rs.next()) {
CryptoKey secretKey = secretHelper.getSecret(rs.getBytes("secret"));
if (secretKey instanceof AesCryptoKey) {
log.warn("Legacy AES crypto key: findPrivateData[{}, {}, {}]", new Object[] { serviceType, project, metadataKey });
}
byte[] plaintext = FathomdbCrypto.decrypt(secretKey, rs.getBytes("data"));
String value = Utf8.toString(plaintext);
values.add(value);
}
} catch (SQLException e) {
throw new RepositoryException("Error running query", e);
} finally {
JdbcUtils.safeClose(rs);
db.close();
}
if (values.size() == 0) {
return null;
}
if (values.size() != 1) {
throw new IllegalStateException("Found duplicate results for primary key");
}
return values.get(0);
}
use of com.fathomdb.crypto.CryptoKey in project platformlayer by platformlayer.
the class SecretProvider method from.
public static SecretProvider from(final ProjectInfo project) {
if (project == null) {
throw new IllegalArgumentException();
}
return new SecretProvider() {
@Override
public CryptoKey getItemSecret(SecretInfo secret) {
SecretStore secretStore = new SecretStore(secret.getEncoded());
CryptoKey secretKey = secretStore.getSecretFromProject(project);
return secretKey;
}
};
}
Aggregations