/** * Encrypt the data. * @param key The key. * @param bytes The data * @return byte[] The encrypted data * @throws HttpException If {@link Cipher.doFinal(byte[])} fails */ private byte[] encrypt(byte[] key, byte[] bytes) throws AuthenticationException { Cipher ecipher = getCipher(key); try { byte[] enc = ecipher.doFinal(bytes); return enc; } catch (IllegalBlockSizeException e) { throw new AuthenticationException("Invalid block size for DES encryption.", e); } catch (BadPaddingException e) { throw new AuthenticationException("Data not padded correctly for DES encryption.", e); } }
/** * Return the cipher for the specified key. * @param key The key. * @return Cipher The cipher. * @throws AuthenticationException If the cipher cannot be retrieved. */ private Cipher getCipher(byte[] key) throws AuthenticationException { try { final Cipher ecipher = Cipher.getInstance("DES/ECB/NoPadding"); key = setupKey(key); ecipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "DES")); return ecipher; } catch (NoSuchAlgorithmException e) { throw new AuthenticationException("DES encryption is not available.", e); } catch (InvalidKeyException e) { throw new AuthenticationException("Invalid key for DES encryption.", e); } catch (NoSuchPaddingException e) { throw new AuthenticationException( "NoPadding option for DES is not available.", e); } }
throw new AuthenticationException( "Unsupported qop in HTTP Digest authentication"); md5Helper = MessageDigest.getInstance(digAlg); } catch (Exception e) { throw new AuthenticationException( "Unsupported algorithm in HTTP Digest authentication: " + digAlg);
throw new AuthenticationException(id + " authorization challenge expected, but not found");
/** Read a byte from a position within the message buffer */ protected byte readByte(final int position) throws AuthenticationException { if (messageContents.length < position + 1) { throw new AuthenticationException("NTLM: Message too short"); } return messageContents[position]; }
/** * Find the character set based on the flags. * @param flags is the flags. * @return the character set. */ private static Charset getCharset(final int flags) throws AuthenticationException { if ((flags & FLAG_REQUEST_UNICODE_ENCODING) == 0) { return DEFAULT_CHARSET; } else { if (UNICODE_LITTLE_UNMARKED == null) { throw new AuthenticationException( "Unicode not supported" ); } return UNICODE_LITTLE_UNMARKED; } }
/** Read a bunch of bytes from a position in the message buffer */ protected void readBytes(final byte[] buffer, final int position) throws AuthenticationException { if (messageContents.length < position + buffer.length) { throw new AuthenticationException("NTLM: Message too short"); } System.arraycopy(messageContents, position, buffer, 0, buffer.length); }
private String generateNonce() throws AuthenticationException { try { SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); byte[] temp = new byte[NONCE_LENGTH]; sr.nextBytes(temp); String n = new String(Hex.encodeHex(temp)); return n; } catch (Exception e) { throw new AuthenticationException(e.getMessage(), e); } }
private String generateNonce() throws AuthenticationException { try { SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); byte[] temp = new byte[NONCE_LENGTH]; sr.nextBytes(temp); String n = new String(Hex.encodeHex(temp)); return n; } catch (Exception e) { throw new AuthenticationException(e.getMessage(), e); } }
/** Calculates RC4 */ static byte[] RC4(final byte[] value, final byte[] key) throws AuthenticationException { try { final Cipher rc4 = Cipher.getInstance("RC4"); rc4.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "RC4")); return rc4.doFinal(value); } catch (final Exception e) { throw new AuthenticationException(e.getMessage(), e); } }
/** * Encrypt the data. * @param key The key. * @param bytes The data * @return byte[] The encrypted data * @throws HttpException If {@link Cipher.doFinal(byte[])} fails */ private byte[] encrypt(byte[] key, byte[] bytes) throws AuthenticationException { Cipher ecipher = getCipher(key); try { byte[] enc = ecipher.doFinal(bytes); return enc; } catch (IllegalBlockSizeException e) { throw new AuthenticationException("Invalid block size for DES encryption.", e); } catch (BadPaddingException e) { throw new AuthenticationException("Data not padded correctly for DES encryption.", e); } }
/** * Encrypt the data. * @param key The key. * @param bytes The data * @return byte[] The encrypted data * @throws HttpException If {@link Cipher.doFinal(byte[])} fails */ private byte[] encrypt(byte[] key, byte[] bytes) throws AuthenticationException { Cipher ecipher = getCipher(key); try { byte[] enc = ecipher.doFinal(bytes); return enc; } catch (IllegalBlockSizeException e) { throw new AuthenticationException("Invalid block size for DES encryption.", e); } catch (BadPaddingException e) { throw new AuthenticationException("Data not padded correctly for DES encryption.", e); } }
private String generatePasswordDigest(String password, String nonce, AtomDate date) throws AuthenticationException { String temp = nonce + date.getValue() + password; try { MessageDigest md = MessageDigest.getInstance("SHA1"); return new String(Base64.encodeBase64(md.digest(temp.getBytes()))); } catch (Exception e) { throw new AuthenticationException(e.getMessage(), e); } }
/** * Encrypt the data. * @param key The key. * @param bytes The data * @return byte[] The encrypted data * @throws HttpException If {@link Cipher.doFinal(byte[])} fails */ private byte[] encrypt(byte[] key, byte[] bytes) throws AuthenticationException { Cipher ecipher = getCipher(key); try { byte[] enc = ecipher.doFinal(bytes); return enc; } catch (IllegalBlockSizeException e) { throw new AuthenticationException("Invalid block size for DES encryption.", e); } catch (BadPaddingException e) { throw new AuthenticationException("Data not padded correctly for DES encryption.", e); } }
/** * Encrypt the data. * @param key The key. * @param bytes The data * @return byte[] The encrypted data * @throws HttpException If {@link Cipher.doFinal(byte[])} fails */ private byte[] encrypt(byte[] key, byte[] bytes) throws AuthenticationException { Cipher ecipher = getCipher(key); try { byte[] enc = ecipher.doFinal(bytes); return enc; } catch (IllegalBlockSizeException e) { throw new AuthenticationException("Invalid block size for DES encryption.", e); } catch (BadPaddingException e) { throw new AuthenticationException("Data not padded correctly for DES encryption.", e); } }
public byte[] decryptAndVerifySignedMessage( final byte[] inMessage ) throws AuthenticationException { final byte[] signature = new byte[16]; System.arraycopy( inMessage, 0, signature, 0, signature.length ); final byte[] encryptedMessage = new byte[inMessage.length - 16]; System.arraycopy( inMessage, 16, encryptedMessage, 0, encryptedMessage.length ); final byte[] cleartextMessage = decrypt( encryptedMessage ); if ( !validateSignature( signature, cleartextMessage ) ) { throw new AuthenticationException( "Wrong signature" ); } advanceMessageSequence(); return cleartextMessage; }
/** * Creates the LMv2 Hash of the user's password. * * @return The LMv2 Hash, used in the calculation of the NTLMv2 and LMv2 * Responses. */ private static byte[] lmv2Hash(final String domain, final String user, final byte[] ntlmHash) throws AuthenticationException { if (UNICODE_LITTLE_UNMARKED == null) { throw new AuthenticationException("Unicode not supported"); } final HMACMD5 hmacMD5 = new HMACMD5(ntlmHash); // Upper case username, upper case domain! hmacMD5.update(user.toUpperCase(Locale.ROOT).getBytes(UNICODE_LITTLE_UNMARKED)); if (domain != null) { hmacMD5.update(domain.toUpperCase(Locale.ROOT).getBytes(UNICODE_LITTLE_UNMARKED)); } return hmacMD5.getOutput(); }
/** * Creates the NTLMv2 Hash of the user's password. * * @return The NTLMv2 Hash, used in the calculation of the NTLMv2 and LMv2 * Responses. */ private static byte[] ntlmv2Hash(final String domain, final String user, final byte[] ntlmHash) throws AuthenticationException { if (UNICODE_LITTLE_UNMARKED == null) { throw new AuthenticationException("Unicode not supported"); } final HMACMD5 hmacMD5 = new HMACMD5(ntlmHash); // Upper case username, mixed case target!! hmacMD5.update(user.toUpperCase(Locale.ROOT).getBytes(UNICODE_LITTLE_UNMARKED)); if (domain != null) { hmacMD5.update(domain.getBytes(UNICODE_LITTLE_UNMARKED)); } return hmacMD5.getOutput(); }
/** * Creates the NTLM Hash of the user's password. * * @param password * The password. * * @return The NTLM Hash of the given password, used in the calculation of * the NTLM Response and the NTLMv2 and LMv2 Hashes. */ private static byte[] ntlmHash(final String password) throws AuthenticationException { if (UNICODE_LITTLE_UNMARKED == null) { throw new AuthenticationException("Unicode not supported"); } final byte[] unicodePassword = password.getBytes(UNICODE_LITTLE_UNMARKED); final MD4 md4 = new MD4(); md4.update(unicodePassword); return md4.getOutput(); }
public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException { String auth = null; if (credentials instanceof UsernamePasswordCredentials) { UsernamePasswordCredentials usercreds = (UsernamePasswordCredentials)credentials; String id = usercreds.getUserName(); String pwd = usercreds.getPassword(); auth = getAuth(id, pwd); } else if (credentials instanceof GoogleLoginAuthCredentials) { GoogleLoginAuthCredentials gcreds = (GoogleLoginAuthCredentials)credentials; service = gcreds.getService(); auth = gcreds.getAuth(); } else { throw new AuthenticationException("Cannot use credentials for GoogleLogin authentication"); } StringBuffer buf = new StringBuffer("GoogleLogin "); buf.append(auth); return buf.toString(); }