Search in sources :

Example 1 with EncryptedMessage

use of com.tremolosecurity.provisioning.util.EncryptedMessage in project OpenUnison by TremoloSecurity.

the class LoadToken method loadToken.

@Override
public Object loadToken(AuthInfo user, HttpSession session) throws Exception {
    HashMap<String, String> token = new HashMap<String, String>();
    Attribute attr = user.getAttribs().get(this.attributeName);
    if (attr != null) {
        String json = attr.getValues().get(0);
        Gson gson = new Gson();
        EncryptedMessage em = gson.fromJson(json, EncryptedMessage.class);
        SecretKey key = GlobalEntries.getGlobalEntries().getConfigManager().getSecretKey(this.encryptionKey);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec spec = new IvParameterSpec(em.getIv());
        cipher.init(Cipher.DECRYPT_MODE, key, spec);
        byte[] bytes = cipher.doFinal(em.getMsg());
        String password = new String(bytes);
        token.put("Temporary Password", password);
    } else {
        token.put("Temporary Password", "No password found");
    }
    return token;
}
Also used : SecretKey(javax.crypto.SecretKey) HashMap(java.util.HashMap) Attribute(com.tremolosecurity.saml.Attribute) EncryptedMessage(com.tremolosecurity.provisioning.util.EncryptedMessage) Gson(com.google.gson.Gson) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Example 2 with EncryptedMessage

use of com.tremolosecurity.provisioning.util.EncryptedMessage in project OpenUnison by TremoloSecurity.

the class UnisonMessageListener method onMessage.

@Override
public void onMessage(Message msg) {
    try {
        TextMessage smsg = (TextMessage) msg;
        if (smsg.getBooleanProperty("unisonignore")) {
            if (logger.isDebugEnabled()) {
                logger.debug("ignoring message");
            }
            smsg.acknowledge();
            return;
        }
        ConfigManager cfgMgr = (ConfigManager) GlobalEntries.getGlobalEntries().get(ProxyConstants.CONFIG_MANAGER);
        Gson gson = new Gson();
        Object obj;
        if (this.isEncrypted()) {
            EncryptedMessage em = gson.fromJson(smsg.getText(), EncryptedMessage.class);
            obj = cfgMgr.getProvisioningEngine().decryptObject(em);
        } else {
            obj = JsonReader.jsonToJava(smsg.getText());
        }
        this.onMessage(cfgMgr, obj, msg);
        msg.acknowledge();
    } catch (Throwable t) {
        logger.error("Unable to run listener", t);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter baout = new PrintWriter(baos);
        t.printStackTrace(baout);
        baout.flush();
        baout.close();
        StringBuffer b = new StringBuffer();
        b.append("Could not run listener").append(new String(baos.toByteArray()));
        throw new RuntimeException(b.toString(), t);
    }
}
Also used : EncryptedMessage(com.tremolosecurity.provisioning.util.EncryptedMessage) Gson(com.google.gson.Gson) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TextMessage(javax.jms.TextMessage) ConfigManager(com.tremolosecurity.config.util.ConfigManager) PrintWriter(java.io.PrintWriter)

Example 3 with EncryptedMessage

use of com.tremolosecurity.provisioning.util.EncryptedMessage in project OpenUnison by TremoloSecurity.

the class SendMessageThread method encryptObject.

@Override
public EncryptedMessage encryptObject(Object o) throws ProvisioningException {
    SecretKey key = this.cfgMgr.getSecretKey(this.cfgMgr.getCfg().getProvisioning().getQueueConfig().getEncryptionKeyName());
    if (key == null) {
        throw new ProvisioningException("Queue message encryption key not found");
    }
    try {
        String json = JsonWriter.objectToJson(o);
        byte[] encoded = json.getBytes("UTF-8");
        EncryptedMessage msg = new EncryptedMessage();
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        msg.setMsg(cipher.doFinal(encoded));
        msg.setIv(cipher.getIV());
        return msg;
    } catch (IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
        throw new ProvisioningException("Could not encrypt message", e);
    }
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) EncryptedMessage(com.tremolosecurity.provisioning.util.EncryptedMessage) Cipher(javax.crypto.Cipher)

Example 4 with EncryptedMessage

use of com.tremolosecurity.provisioning.util.EncryptedMessage in project OpenUnison by TremoloSecurity.

the class WebAuthnUtils method lookupWebAuthnUserData.

public static WebAuthnUserData lookupWebAuthnUserData(AuthInfo userData, String attributeName, String encryptionKeyName) throws ServletException {
    Attribute encData = userData.getAttribs().get(attributeName);
    if (encData == null) {
        return null;
    } else {
        try {
            String encAuthData = encData.getValues().get(0);
            String encryptedAuth = inflate(encAuthData);
            SecretKey key = GlobalEntries.getGlobalEntries().getConfigManager().getSecretKey(encryptionKeyName);
            if (key == null) {
                throw new Exception("encryption key not found");
            }
            EncryptedMessage msg = gson.fromJson(encryptedAuth, EncryptedMessage.class);
            IvParameterSpec spec = new IvParameterSpec(msg.getIv());
            Cipher cipher;
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, spec);
            byte[] bytes = cipher.doFinal(msg.getMsg());
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
            WebAuthnUserData webAuthnData = (WebAuthnUserData) ois.readObject();
            return webAuthnData;
        } catch (Exception e) {
            throw new ServletException("Could not extract webauthn user data", e);
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) SecretKey(javax.crypto.SecretKey) Attribute(com.tremolosecurity.saml.Attribute) ByteArrayInputStream(java.io.ByteArrayInputStream) EncryptedMessage(com.tremolosecurity.provisioning.util.EncryptedMessage) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 5 with EncryptedMessage

use of com.tremolosecurity.provisioning.util.EncryptedMessage in project OpenUnison by TremoloSecurity.

the class WebAuthnUtils method storeWebAuthnUserData.

public static void storeWebAuthnUserData(WebAuthnUserData webAuthnUserData, String encryptionKeyName, AuthInfo userData, String workflowName, String uidAttributeName, String challengeStoreAttribute) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(webAuthnUserData);
    EncryptedMessage msg = new EncryptedMessage();
    SecretKey key = GlobalEntries.getGlobalEntries().getConfigManager().getSecretKey(encryptionKeyName);
    if (key == null) {
        throw new Exception("User data message encryption key not found");
    }
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    msg.setMsg(cipher.doFinal(baos.toByteArray()));
    msg.setIv(cipher.getIV());
    baos = new ByteArrayOutputStream();
    DeflaterOutputStream compressor = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_COMPRESSION, true));
    Gson gson = new Gson();
    compressor.write(gson.toJson(msg).getBytes("UTF-8"));
    compressor.flush();
    compressor.close();
    String b64 = new String(java.util.Base64.getEncoder().encodeToString(baos.toByteArray()));
    userData.getAttribs().put(challengeStoreAttribute, new Attribute(challengeStoreAttribute, b64));
    WFCall wc = new WFCall();
    wc.setName(workflowName);
    wc.setUidAttributeName(uidAttributeName);
    TremoloUser tu = new TremoloUser();
    tu.setUid(userData.getAttribs().get(uidAttributeName).getValues().get(0));
    tu.getAttributes().add(new Attribute(uidAttributeName, userData.getAttribs().get(uidAttributeName).getValues().get(0)));
    tu.getAttributes().add(new Attribute(challengeStoreAttribute, b64));
    wc.setUser(tu);
    Map<String, Object> req = new HashMap<String, Object>();
    req.put(ProvisioningParams.UNISON_EXEC_TYPE, ProvisioningParams.UNISON_EXEC_SYNC);
    wc.setRequestParams(req);
    GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getWorkFlow(workflowName).executeWorkflow(wc);
}
Also used : WFCall(com.tremolosecurity.provisioning.service.util.WFCall) Attribute(com.tremolosecurity.saml.Attribute) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) SecretKey(javax.crypto.SecretKey) Deflater(java.util.zip.Deflater) TremoloUser(com.tremolosecurity.provisioning.service.util.TremoloUser) EncryptedMessage(com.tremolosecurity.provisioning.util.EncryptedMessage) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) Cipher(javax.crypto.Cipher)

Aggregations

EncryptedMessage (com.tremolosecurity.provisioning.util.EncryptedMessage)12 Cipher (javax.crypto.Cipher)7 SecretKey (javax.crypto.SecretKey)7 Gson (com.google.gson.Gson)6 IOException (java.io.IOException)6 Attribute (com.tremolosecurity.saml.Attribute)5 InvalidKeyException (java.security.InvalidKeyException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 BadPaddingException (javax.crypto.BadPaddingException)4 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)4 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)4 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 HashMap (java.util.HashMap)3 IvParameterSpec (javax.crypto.spec.IvParameterSpec)3 TextMessage (javax.jms.TextMessage)3 SecurityKeyData (com.google.u2f.server.data.SecurityKeyData)2 TaskHolder (com.tremolosecurity.provisioning.util.TaskHolder)2 KeyHolder (com.tremolosecurity.unison.google.u2f.KeyHolder)2