Search in sources :

Example 6 with CryptoKey

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();
}
Also used : CryptoKey(com.fathomdb.crypto.CryptoKey) IOException(java.io.IOException)

Example 7 with CryptoKey

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();
    }
}
Also used : SQLException(java.sql.SQLException) CryptoKey(com.fathomdb.crypto.CryptoKey) AesCryptoKey(com.fathomdb.crypto.AesCryptoKey) RepositoryException(org.platformlayer.RepositoryException) Tags(org.platformlayer.core.model.Tags) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 8 with CryptoKey

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();
    }
}
Also used : SQLException(java.sql.SQLException) AesCryptoKey(com.fathomdb.crypto.AesCryptoKey) CryptoKey(com.fathomdb.crypto.CryptoKey) RepositoryException(org.platformlayer.RepositoryException) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 9 with CryptoKey

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);
}
Also used : SQLException(java.sql.SQLException) AesCryptoKey(com.fathomdb.crypto.AesCryptoKey) ResultSet(java.sql.ResultSet) AesCryptoKey(com.fathomdb.crypto.AesCryptoKey) CryptoKey(com.fathomdb.crypto.CryptoKey) RepositoryException(org.platformlayer.RepositoryException) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 10 with CryptoKey

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;
        }
    };
}
Also used : SecretInfo(org.platformlayer.core.model.SecretInfo) CryptoKey(com.fathomdb.crypto.CryptoKey)

Aggregations

CryptoKey (com.fathomdb.crypto.CryptoKey)18 RepositoryException (org.platformlayer.RepositoryException)8 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)6 SQLException (java.sql.SQLException)6 AesCryptoKey (com.fathomdb.crypto.AesCryptoKey)5 SecretStore (org.platformlayer.auth.crypto.SecretStore)4 IOException (java.io.IOException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 KeyPair (java.security.KeyPair)2 DirectAuthenticationToken (org.platformlayer.auth.DirectAuthenticationToken)2 DirectAuthenticator (org.platformlayer.auth.DirectAuthenticator)2 UserEntity (org.platformlayer.auth.UserEntity)2 Writer (org.platformlayer.auth.crypto.SecretStore.Writer)2 SecretInfo (org.platformlayer.core.model.SecretInfo)2 ManagedItemId (org.platformlayer.ids.ManagedItemId)2 ProjectAuthorization (org.platformlayer.model.ProjectAuthorization)2 RoleId (org.platformlayer.model.RoleId)2 CliException (com.fathomdb.cli.CliException)1 PublicKey (java.security.PublicKey)1 Certificate (java.security.cert.Certificate)1