@Override public Class<?> loadClass(String name) throws ClassNotFoundException { try { equinoxContainer.checkAdminPermission(this, AdminPermission.CLASS); } catch (SecurityException e) { throw new ClassNotFoundException(name, e); } checkValid(); if (isFragment()) { throw new ClassNotFoundException( "Can not load a class from a fragment bundle: " + this); // $NON-NLS-1$ } try { ModuleClassLoader classLoader = getModuleClassLoader(true); if (classLoader != null) { if (name.length() > 0 && name.charAt(0) == '[') return Class.forName(name, false, classLoader); return classLoader.loadClass(name); } } catch (ClassNotFoundException e) { // This is an equinox-ism. Not sure it is worth it to offer an option to disable ... // On failure attempt to activate lazy activating bundles. if (State.LAZY_STARTING.equals(module.getState())) { try { module.start(StartOptions.LAZY_TRIGGER); } catch (BundleException e1) { equinoxContainer .getLogServices() .log(EquinoxContainer.NAME, FrameworkLogEntry.WARNING, e.getMessage(), e); } } throw e; } throw new ClassNotFoundException( "No class loader available for the bundle: " + this); // $NON-NLS-1$ }
public boolean delete(String filename, UserToken token) { try { String remotePath; if (filename.charAt(0) == '/') { remotePath = filename.substring(1); } else { remotePath = filename; } Envelope env = new Envelope("DELETEF"); // Success env.addObject(remotePath); env.addObject(token); String concat = remotePath + token.toString() + "DELETEF" + nonce; // concatinates all of the objects in envelope byte[] hasharray = concat.getBytes(); // turn the concat into a byte array Mac mac = Mac.getInstance("HmacSHA1"); mac.init(HMACkey); mac.update(hasharray); String stringhash = new String(mac.doFinal(), "UTF8"); // turn the hash into a string for easy comparision! env.addObject(stringhash); env.addObject(nonce); nonce++; byte[] envBytes = Envelope.toByteArray(env); // Encrypt envelope w/ AES Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, AESkey); byte[] cipherBytes = cipher.doFinal(envBytes); output.writeObject(cipherBytes); byte[] responseCipherBytes = (byte[]) input.readObject(); // Decrypt response cipher.init(Cipher.DECRYPT_MODE, AESkey); byte[] responseBytes = cipher.doFinal(responseCipherBytes); env = Envelope.getEnvelopefromBytes(responseBytes); System.out.println(env.getMessage()); if ((Integer) env.getObjContents().get(1) == nonce) { String hash = (String) env.getObjContents().get(0); concat = env.getMessage() + nonce; // reconstructs the hash hasharray = concat.getBytes(); mac = Mac.getInstance("HmacSHA1"); File HASHfile = new File("FHASHKey.bin"); FileInputStream fis = new FileInputStream(HASHfile); ObjectInputStream ois = new ObjectInputStream(fis); Key HMACkey = (Key) ois.readObject(); mac.init(HMACkey); mac.update(hasharray); String newhash = new String(mac.doFinal(), "UTF8"); nonce++; if (hash.equals(newhash) != true) // check hashes for equality { System.out.println("HASH EQUALITY FAIL"); return false; } if (env.getMessage().compareTo("OK") == 0) { System.out.printf("File %s deleted successfully\n", filename); } else { System.out.printf("Error deleting file %s (%s)\n", filename, env.getMessage()); return false; } } } catch (IllegalBlockSizeException ex) { ex.printStackTrace(System.err); } catch (BadPaddingException ex) { ex.printStackTrace(System.err); } catch (InvalidKeyException ex) { ex.printStackTrace(System.err); } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(System.err); } catch (NoSuchPaddingException ex) { ex.printStackTrace(System.err); } catch (IOException e1) { e1.printStackTrace(System.err); } catch (ClassNotFoundException e1) { e1.printStackTrace(System.err); } return true; }
public boolean download( String sourceFile, String destFile, UserToken token, HashMap<String, ArrayList<Key>> keys) { try { destFile = "." + destFile; if (sourceFile.charAt(0) == '/') { sourceFile = sourceFile.substring(1); } File file = new File(destFile); if (!file.exists()) { file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); Envelope env = new Envelope("DOWNLOADF"); // Success env.addObject(sourceFile); env.addObject(token); String concat = sourceFile + token.toString() + "DOWNLOADF" + nonce; // concatinates all of the objects in envelope byte[] hasharray = concat.getBytes(); // turn the concat into a byte array Mac mac = Mac.getInstance("HmacSHA1"); mac.init(HMACkey); mac.update(hasharray); String stringhash = new String(mac.doFinal(), "UTF8"); // turn the hash into a string for easy comparision! env.addObject(stringhash); env.addObject(nonce); nonce++; byte[] envBytes = Envelope.toByteArray(env); // Encrypt envelope w/ AES Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, AESkey); byte[] cipherBytes = cipher.doFinal(envBytes); output.writeObject(cipherBytes); // here in download byte[] responseCipherBytes = (byte[]) input.readObject(); // Decrypt response cipher.init(Cipher.DECRYPT_MODE, AESkey); byte[] responseBytes = cipher.doFinal(responseCipherBytes); env = Envelope.getEnvelopefromBytes(responseBytes); ShareFile sf = (ShareFile) env.getObjContents().get(2); int keyNum = sf.getKeyNum(); ArrayList<Key> groupKeys = keys.get(sf.getGroup()); Key key = groupKeys.get(keyNum); byte[] initialVector = sf.getIV(); IvParameterSpec ivs = new IvParameterSpec(initialVector); byte[] decryptBuf = new byte[1024]; while (env.getMessage().compareTo("CHUNK") == 0 && (Integer) env.getObjContents().get(4) == nonce) { String hash = (String) env.getObjContents().get(3); concat = (Integer) env.getObjContents().get(1) + env.getMessage() + nonce; // reconstructs the hash System.out.println("Concat:" + concat); hasharray = concat.getBytes(); mac = Mac.getInstance("HmacSHA1"); File HASHfile = new File("FHASHKey.bin"); FileInputStream fis = new FileInputStream(HASHfile); ObjectInputStream ois = new ObjectInputStream(fis); HMACkey = (Key) ois.readObject(); mac.init(HMACkey); mac.update(hasharray); String newhash = new String(mac.doFinal(), "UTF8"); nonce++; // check hashes for equality if (hash.equals(newhash) != true) { System.out.println("HASH EQUALITY FAIL1"); disconnect(); return false; } else { decryptBuf = new byte[1024]; System.out.println("env.getMessage: " + env.getMessage()); cipher = Cipher.getInstance("AES/CBC/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, key, ivs); decryptBuf = cipher.doFinal((byte[]) env.getObjContents().get(0)); // Write encrypted file to disk fos.write(decryptBuf); System.out.printf("."); env = new Envelope("DOWNLOADF"); // Success concat = env.getMessage() + nonce; // concatinates all of the objects in envelope hasharray = concat.getBytes(); // turn the concat into a byte array mac = Mac.getInstance("HmacSHA1"); mac.init(HMACkey); mac.update(hasharray); stringhash = new String( mac.doFinal(), "UTF8"); // turn the hash into a string for easy comparision! env.addObject(stringhash); env.addObject(nonce); nonce++; envBytes = Envelope.toByteArray(env); // Encrypt envelope w/ AES cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, AESkey); cipherBytes = cipher.doFinal(envBytes); output.writeObject(cipherBytes); responseCipherBytes = (byte[]) input.readObject(); // Decrypt response cipher.init(Cipher.DECRYPT_MODE, AESkey); responseBytes = cipher.doFinal(responseCipherBytes); env = Envelope.getEnvelopefromBytes(responseBytes); } } fos.close(); if (env.getMessage().compareTo("EOF") == 0 && (Integer) env.getObjContents().get(1) == nonce) { String hash = (String) env.getObjContents().get(0); concat = env.getMessage() + nonce; // reconstructs the hash hasharray = concat.getBytes(); mac = Mac.getInstance("HmacSHA1"); File HASHfile = new File("FHASHKey.bin"); FileInputStream fis = new FileInputStream(HASHfile); ObjectInputStream ois = new ObjectInputStream(fis); HMACkey = (Key) ois.readObject(); mac.init(HMACkey); mac.update(hasharray); String newhash = new String(mac.doFinal(), "UTF8"); if (hash.equals(newhash) != true) // check hashes for equality { System.out.println("HASH EQUALITY FAIL2"); disconnect(); } fos.close(); System.out.printf("\nTransfer successful file %s\n", sourceFile); nonce++; env = new Envelope("OK"); // Success concat = env.getMessage() + nonce; // concatinates all of the objects in envelope hasharray = concat.getBytes(); // turn the concat into a byte array mac = Mac.getInstance("HmacSHA1"); mac.init(HMACkey); mac.update(hasharray); stringhash = new String( mac.doFinal(), "UTF8"); // turn the hash into a string for easy comparision! env.addObject(stringhash); env.addObject(nonce); nonce++; envBytes = Envelope.toByteArray(env); // Encrypt envelope w/ AES cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, AESkey); cipherBytes = cipher.doFinal(envBytes); output.writeObject(cipherBytes); } else if ((Integer) env.getObjContents().get(1) != nonce) { System.out.println("Nonce FAIL DOWNLOADF"); disconnect(); return false; } else { System.out.printf("Error reading file %s (%s)\n", sourceFile, env.getMessage()); file.delete(); return false; } } else { System.out.printf("Error couldn't create file %s\n", destFile); return false; } } catch (InvalidAlgorithmParameterException ex) { Logger.getLogger(FileClient.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalBlockSizeException ex) { Logger.getLogger(FileClient.class.getName()).log(Level.SEVERE, null, ex); System.out.println(1); } catch (BadPaddingException ex) { Logger.getLogger(FileClient.class.getName()).log(Level.SEVERE, null, ex); System.out.println(2); } catch (InvalidKeyException ex) { Logger.getLogger(FileClient.class.getName()).log(Level.SEVERE, null, ex); System.out.println(3); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(FileClient.class.getName()).log(Level.SEVERE, null, ex); System.out.println(4); } catch (NoSuchPaddingException ex) { Logger.getLogger(FileClient.class.getName()).log(Level.SEVERE, null, ex); System.out.println(5); } catch (IOException e1) { System.out.printf("Error couldn't create file %s\n", destFile); return false; } catch (ClassNotFoundException e1) { e1.printStackTrace(System.err); } return true; }
public static void main(String[] args) { try { if (args[0].equals("-genkey")) { KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA"); SecureRandom random = new SecureRandom(); pairgen.initialize(KEYSIZE, random); KeyPair keyPair = pairgen.generateKeyPair(); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1])); out.writeObject(keyPair.getPublic()); out.close(); out = new ObjectOutputStream(new FileOutputStream(args[2])); out.writeObject(keyPair.getPrivate()); out.close(); } else if (args[0].equals("-encrypt")) { KeyGenerator keygen = KeyGenerator.getInstance("AES"); SecureRandom random = new SecureRandom(); keygen.init(random); SecretKey key = keygen.generateKey(); // wrap with RSA public key ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3])); Key publicKey = (Key) keyIn.readObject(); keyIn.close(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.WRAP_MODE, publicKey); byte[] wrappedKey = cipher.wrap(key); DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2])); out.writeInt(wrappedKey.length); out.write(wrappedKey); InputStream in = new FileInputStream(args[1]); cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); crypt(in, out, cipher); in.close(); out.close(); } else { DataInputStream in = new DataInputStream(new FileInputStream(args[1])); int length = in.readInt(); byte[] wrappedKey = new byte[length]; in.read(wrappedKey, 0, length); // unwrap with RSA private key ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3])); Key privateKey = (Key) keyIn.readObject(); keyIn.close(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.UNWRAP_MODE, privateKey); Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY); OutputStream out = new FileOutputStream(args[2]); cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); crypt(in, out, cipher); in.close(); out.close(); } } catch (IOException e) { e.printStackTrace(); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
@Override public void contextInitialized(ServletContextEvent arg0) { final String S_ProcName = "contextInitialized"; Properties props = System.getProperties(); if (null == CFBamSchemaPool.getSchemaPool()) { try { Context ctx = new InitialContext(); String poolClassName = (String) ctx.lookup("java:comp/env/CFBam24PoolClass"); if ((poolClassName == null) || (poolClassName.length() <= 0)) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "JNDI lookup for CFBam24PoolClass"); } Class poolClass = Class.forName(poolClassName); if (poolClass == null) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "CFBam24PoolClass \"" + poolClassName + "\" not found."); } Object obj = poolClass.newInstance(); if (obj instanceof CFBamSchemaPool) { CFBamSchemaPool newPool = (CFBamSchemaPool) obj; newPool.setConfigurationFile(null); newPool.setJndiName("java:comp/env/CFBam24Connection"); CFBamSchemaPool.setSchemaPool(newPool); } else { throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Problems constructing an instance of " + poolClassName); } String smtpHost = (String) ctx.lookup("java:comp/env/CFBam24SmtpHost"); if ((smtpHost == null) || (smtpHost.length() <= 0)) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "JNDI lookup for CFBam24SmtpHost"); } props.setProperty("mail.smtp.host", smtpHost); String smtpStartTLS = (String) ctx.lookup("java:comp/env/CFBam24SmtpStartTLS"); if ((smtpHost == null) || (smtpHost.length() <= 0)) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "JNDI lookup for CFBam24SmtpStartTLS"); } props.setProperty("mail.smtp.starttls.enable", smtpStartTLS); String smtpSocketFactoryClass = (String) ctx.lookup("java:comp/env/CFBam24SmtpSocketFactoryClass"); if ((smtpSocketFactoryClass == null) || (smtpSocketFactoryClass.length() <= 0)) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "JNDI lookup for CFBam24SmtpSocketFactoryClass"); } props.setProperty("mail.smtp.socketFactory.class", smtpSocketFactoryClass); props.setProperty("mail.smtp.socketFactory.fallback", "false"); String smtpPort = (String) ctx.lookup("java:comp/env/CFBam24SmtpPort"); if ((smtpPort == null) || (smtpPort.length() <= 0)) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "JNDI lookup for CFBam24SmtpPort"); } props.setProperty("mail.smtp.port", smtpPort); props.setProperty("mail.smtp.socketFactory.port", smtpPort); props.setProperty("mail.smtps.auth", "true"); props.put("mail.smtps.quitwait", "false"); String smtpEmailFrom = (String) ctx.lookup("java:comp/env/CFBam24SmtpEmailFrom"); if ((smtpEmailFrom == null) || (smtpEmailFrom.length() <= 0)) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "JNDI lookup for CFBam24SmtpEmailFrom"); } smtpUsername = (String) ctx.lookup("java:comp/env/CFBam24SmtpUsername"); if ((smtpUsername == null) || (smtpUsername.length() <= 0)) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "JNDI lookup for CFBam24SmtpUsername"); } smtpPassword = (String) ctx.lookup("java:comp/env/CFBam24SmtpPassword"); if ((smtpPassword == null) || (smtpPassword.length() <= 0)) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "JNDI lookup for CFBam24SmtpPassword"); } String serverKeyStore; try { serverKeyStore = (String) ctx.lookup("java:comp/env/CFBam24ServerKeyStore"); } catch (NamingException e) { serverKeyStore = null; } String keyStorePassword; try { keyStorePassword = (String) ctx.lookup("java:comp/env/CFBam24KeyStorePassword"); } catch (NamingException e) { keyStorePassword = null; } String keyName; try { keyName = (String) ctx.lookup("java:comp/env/CFBam24KeyName"); } catch (NamingException e) { keyName = null; } String keyPassword; try { keyPassword = (String) ctx.lookup("java:comp/env/CFBam24KeyPassword"); } catch (NamingException e) { keyPassword = null; } if (((serverKeyStore != null) && (serverKeyStore.length() > 0)) && (keyStorePassword != null) && ((keyName != null) && (keyName.length() > 0)) && (keyPassword != null)) { KeyStore keyStore = null; File keystoreFile = new File(serverKeyStore); if (!keystoreFile.exists()) { throw CFLib.getDefaultExceptionFactory() .newUsageException( getClass(), S_ProcName, "CFBam24ServerKeyStore file \"" + serverKeyStore + "\" does not exist."); } else if (!keystoreFile.isFile()) { throw CFLib.getDefaultExceptionFactory() .newUsageException( getClass(), S_ProcName, "CFBam24ServerKeyStore file \"" + serverKeyStore + "\" is not a file."); } else if (!keystoreFile.canRead()) { throw CFLib.getDefaultExceptionFactory() .newUsageException( getClass(), S_ProcName, "Permission denied attempting to read CFBam24ServerKeyStore file \"" + serverKeyStore + "\"."); } try { keyStore = KeyStore.getInstance("jceks"); char[] caPassword = keyStorePassword.toCharArray(); FileInputStream input = new FileInputStream(serverKeyStore); keyStore.load(input, caPassword); input.close(); Certificate publicKeyCertificate = keyStore.getCertificate(keyName); if (publicKeyCertificate == null) { throw CFLib.getDefaultExceptionFactory() .newUsageException( getClass(), S_ProcName, "Could not read CFBam24KeyName \"" + keyName + "\" from CFBam24ServerKeyStore file \"" + serverKeyStore + "\"."); } publicKey = publicKeyCertificate.getPublicKey(); char[] caKeyPassword = keyPassword.toCharArray(); Key key = keyStore.getKey(keyName, caKeyPassword); if (key instanceof PrivateKey) { privateKey = (PrivateKey) key; } else { throw CFLib.getDefaultExceptionFactory() .newUnsupportedClassException(getClass(), S_ProcName, "key", key, "PrivateKey"); } getServerInfo(); } catch (CertificateException x) { publicKey = null; privateKey = null; throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Could not open keystore due to CertificateException -- " + x.getMessage(), x); } catch (IOException x) { publicKey = null; privateKey = null; throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Could not open keystore due to IOException -- " + x.getMessage(), x); } catch (KeyStoreException x) { publicKey = null; privateKey = null; throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Could not open keystore due to KeyStoreException -- " + x.getMessage(), x); } catch (NoSuchAlgorithmException x) { publicKey = null; privateKey = null; throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Could not open keystore due to NoSuchAlgorithmException -- " + x.getMessage(), x); } catch (UnrecoverableKeyException x) { publicKey = null; privateKey = null; throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Could not access key due to UnrecoverableKeyException -- " + x.getMessage(), x); } catch (RuntimeException x) { publicKey = null; privateKey = null; throw x; } } else if ((serverKeyStore != null) || (keyStorePassword != null) || (keyName != null) || (keyPassword != null)) { publicKey = null; privateKey = null; throw CFLib.getDefaultExceptionFactory() .newUsageException( getClass(), S_ProcName, "All or none of CFBam24ServerKeyStore, " + "CFBam24KeyStorePassword, " + "CFBam24KeyName, and " + "CFBam24KeyPassword must be configured"); } else { getServerInfo(); try { serverInfo.initServerKeys(); } catch (Exception x) { throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Caught " + x.getClass().getName() + " during initServerKeys() -- " + x.getMessage(), x); } } } catch (ClassNotFoundException e) { publicKey = null; privateKey = null; throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Caught ClassNotFoundException -- " + e.getMessage(), e); } catch (IllegalAccessException e) { publicKey = null; privateKey = null; throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Caught IllegalAccessException trying to construct newInstance() -- " + e.getMessage(), e); } catch (InstantiationException e) { publicKey = null; privateKey = null; throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Caught InstantiationException trying to construct newInstance() -- " + e.getMessage(), e); } catch (NamingException e) { publicKey = null; privateKey = null; throw CFLib.getDefaultExceptionFactory() .newRuntimeException( getClass(), S_ProcName, "Caught NamingException -- " + e.getMessage(), e); } } }