use of com.tremolosecurity.unison.google.u2f.KeyHolder in project OpenUnison by TremoloSecurity.
the class U2fUtil method loadUserKeys.
public static List<SecurityKeyData> loadUserKeys(AuthInfo userData, String challengeStoreAttribute, String encyrptionKeyName) throws Exception, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
Attribute challengeAttr = userData.getAttribs().get(challengeStoreAttribute);
Type t = new TypeToken<List<KeyHolder>>() {
}.getType();
ArrayList<SecurityKeyData> devices = new ArrayList<SecurityKeyData>();
if (challengeAttr != null) {
SecretKey key = GlobalEntries.getGlobalEntries().getConfigManager().getSecretKey(encyrptionKeyName);
if (key == null) {
throw new Exception("Queue message encryption key not found");
}
EncryptedMessage msg = gson.fromJson(inflate(challengeAttr.getValues().get(0)), 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());
String json = new String(bytes);
java.util.List<KeyHolder> fromJSON = gson.fromJson(json, t);
for (KeyHolder kh : fromJSON) {
devices.add(new SecurityKeyData(kh.getEnrollmentTime(), kh.getKeyHandle(), kh.getPublicKey(), null, kh.getCounter()));
}
}
return devices;
}
use of com.tremolosecurity.unison.google.u2f.KeyHolder in project OpenUnison by TremoloSecurity.
the class U2fUtil method encode.
public static String encode(List<SecurityKeyData> devices, String encyrptionKeyName) throws Exception {
ArrayList<KeyHolder> keys = new ArrayList<KeyHolder>();
for (SecurityKeyData dr : devices) {
KeyHolder kh = new KeyHolder();
kh.setCounter(dr.getCounter());
kh.setEnrollmentTime(dr.getEnrollmentTime());
kh.setKeyHandle(dr.getKeyHandle());
kh.setPublicKey(dr.getPublicKey());
kh.setTransports(dr.getTransports());
keys.add(kh);
}
String json = gson.toJson(keys);
EncryptedMessage msg = new EncryptedMessage();
SecretKey key = GlobalEntries.getGlobalEntries().getConfigManager().getSecretKey(encyrptionKeyName);
if (key == null) {
throw new Exception("Queue message encryption key not found");
}
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
msg.setMsg(cipher.doFinal(json.getBytes("UTF-8")));
msg.setIv(cipher.getIV());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DeflaterOutputStream compressor = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_COMPRESSION, true));
compressor.write(gson.toJson(msg).getBytes("UTF-8"));
compressor.flush();
compressor.close();
String b64 = new String(Base64.encodeBase64(baos.toByteArray()));
return b64;
}
Aggregations