use of java.security.SecureRandom in project camel by apache.
the class SecureRandomParameters method createSecureRandom.
/**
* Returns a {@code SecureRandom} instance initialized using the configured
* algorithm and provider, if specified.
*
* @return the configured instance
*
* @throws GeneralSecurityException if the algorithm is not implemented by
* any registered provider or if the identified provider does
* not exist.
*/
public SecureRandom createSecureRandom() throws GeneralSecurityException {
LOG.debug("Creating SecureRandom from SecureRandomParameters: {}", this);
SecureRandom secureRandom;
if (this.getProvider() != null) {
secureRandom = SecureRandom.getInstance(this.parsePropertyValue(this.getAlgorithm()), this.parsePropertyValue(this.getProvider()));
} else {
secureRandom = SecureRandom.getInstance(this.parsePropertyValue(this.getAlgorithm()));
}
LOG.debug("SecureRandom [{}] is using provider [{}] and algorithm [{}].", new Object[] { secureRandom, secureRandom.getProvider(), secureRandom.getAlgorithm() });
return secureRandom;
}
use of java.security.SecureRandom in project camel by apache.
the class AbstractJsseParametersTest method createPropertiesPlaceholderAwareContext.
protected CamelContext createPropertiesPlaceholderAwareContext() throws Exception {
Properties supplementalProperties = new Properties();
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
SecureRandom sr = null;
try {
sr = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
// Ignore
}
SSLContext sslc = SSLContext.getInstance("TLS");
sslc.init(null, null, null);
SSLSocket socket = (SSLSocket) sslc.getSocketFactory().createSocket();
supplementalProperties.setProperty("keyStoreParameters.type", KeyStore.getDefaultType());
supplementalProperties.setProperty("keyStoreParameters.provider", ks.getProvider().getName());
supplementalProperties.setProperty("keyManagersParameters.algorithm", KeyManagerFactory.getDefaultAlgorithm());
supplementalProperties.setProperty("keyManagersParameters.provider", kmf.getProvider().getName());
supplementalProperties.setProperty("trustManagersParameters.algorithm", TrustManagerFactory.getDefaultAlgorithm());
supplementalProperties.setProperty("trustManagersParameters.provider", tmf.getProvider().getName());
if (sr != null) {
supplementalProperties.setProperty("secureRandomParameters.algorithm", "SHA1PRNG");
supplementalProperties.setProperty("secureRandomParameters.provider", sr.getProvider().getName());
}
supplementalProperties.setProperty("sslContextParameters.provider", sslc.getProvider().getName());
supplementalProperties.setProperty("cipherSuite.0", socket.getSupportedCipherSuites()[0]);
// Have to skip this guy because he doesn't work with TLS as the SSLContext protocol
String ssp = "";
for (String protocol : socket.getSupportedProtocols()) {
if (!"SSLv2Hello".equals(protocol)) {
ssp = protocol;
break;
}
}
supplementalProperties.setProperty("secureSocketProtocol.0", ssp);
return this.createPropertiesPlaceholderAwareContext(supplementalProperties);
}
use of java.security.SecureRandom in project camel by apache.
the class SecureRandomParametersTest method testCreateSecureRandom.
public void testCreateSecureRandom() throws Exception {
if (this.canTest()) {
SecureRandomParameters srp = new SecureRandomParameters();
srp.setAlgorithm("SHA1PRNG");
SecureRandom sr = srp.createSecureRandom();
assertEquals("SHA1PRNG", sr.getAlgorithm());
String providerName = sr.getProvider().getName();
srp.setProvider(providerName);
sr = srp.createSecureRandom();
assertEquals("SHA1PRNG", sr.getAlgorithm());
assertEquals(providerName, sr.getProvider().getName());
}
}
use of java.security.SecureRandom in project camel by apache.
the class LinkedInOAuthRequestFilter method getRefreshToken.
@SuppressWarnings("deprecation")
private String getRefreshToken() {
// disable redirect to avoid loading error redirect URL
webClient.getOptions().setRedirectEnabled(false);
try {
final String csrfId = String.valueOf(new SecureRandom().nextLong());
final String encodedRedirectUri = URLEncoder.encode(oAuthParams.getRedirectUri(), "UTF-8");
final OAuthScope[] scopes = oAuthParams.getScopes();
final String url;
if (scopes == null || scopes.length == 0) {
url = String.format(AUTHORIZATION_URL, oAuthParams.getClientId(), csrfId, encodedRedirectUri);
} else {
final int nScopes = scopes.length;
final StringBuilder builder = new StringBuilder();
int i = 0;
for (OAuthScope scope : scopes) {
builder.append(scope.getValue());
if (++i < nScopes) {
builder.append("%20");
}
}
url = String.format(AUTHORIZATION_URL_WITH_SCOPE, oAuthParams.getClientId(), csrfId, builder.toString(), encodedRedirectUri);
}
HtmlPage authPage;
try {
authPage = webClient.getPage(url);
} catch (FailingHttpStatusCodeException e) {
// only handle errors returned with redirects
if (e.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
final URL location = new URL(e.getResponse().getResponseHeaderValue(HttpHeaders.LOCATION));
final String locationQuery = location.getQuery();
if (locationQuery != null && locationQuery.contains("error=")) {
throw new IOException(URLDecoder.decode(locationQuery).replaceAll("&", ", "));
} else {
// follow the redirect to login form
authPage = webClient.getPage(location);
}
} else {
throw e;
}
}
// look for <div role="alert">
final HtmlDivision div = authPage.getFirstByXPath("//div[@role='alert']");
if (div != null) {
throw new IllegalArgumentException("Error authorizing application: " + div.getTextContent());
}
// submit login credentials
final HtmlForm loginForm = authPage.getFormByName("oauth2SAuthorizeForm");
final HtmlTextInput login = loginForm.getInputByName("session_key");
login.setText(oAuthParams.getUserName());
final HtmlPasswordInput password = loginForm.getInputByName("session_password");
password.setText(oAuthParams.getUserPassword());
final HtmlSubmitInput submitInput = loginForm.getInputByName("authorize");
// validate CSRF and get authorization code
String redirectQuery;
try {
final Page redirectPage = submitInput.click();
redirectQuery = redirectPage.getUrl().getQuery();
} catch (FailingHttpStatusCodeException e) {
// escalate non redirect errors
if (e.getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY) {
throw e;
}
final String location = e.getResponse().getResponseHeaderValue("Location");
redirectQuery = new URL(location).getQuery();
}
if (redirectQuery == null) {
throw new IllegalArgumentException("Redirect response query is null, check username, password and permissions");
}
final Map<String, String> params = new HashMap<String, String>();
final Matcher matcher = QUERY_PARAM_PATTERN.matcher(redirectQuery);
while (matcher.find()) {
params.put(matcher.group(1), matcher.group(2));
}
final String state = params.get("state");
if (!csrfId.equals(state)) {
throw new SecurityException("Invalid CSRF code!");
} else {
// TODO check results??
return params.get("code");
}
} catch (IOException e) {
throw new IllegalArgumentException("Error authorizing application: " + e.getMessage(), e);
}
}
use of java.security.SecureRandom in project hadoop by apache.
the class TestCryptoCodec method testCalculateIV.
/**
* Regression test for IV calculation, see HADOOP-11343
*/
@Test(timeout = 120000)
public void testCalculateIV() throws Exception {
JceAesCtrCryptoCodec codec = new JceAesCtrCryptoCodec();
codec.setConf(conf);
SecureRandom sr = new SecureRandom();
byte[] initIV = new byte[16];
byte[] IV = new byte[16];
long iterations = 1000;
long counter = 10000;
// Overflow test, IV: 00 00 00 00 00 00 00 00 ff ff ff ff ff ff ff ff
for (int i = 0; i < 8; i++) {
initIV[8 + i] = (byte) 0xff;
}
for (long j = 0; j < counter; j++) {
assertIVCalculation(codec, initIV, j, IV);
}
// Random IV and counter sequence test
for (long i = 0; i < iterations; i++) {
sr.nextBytes(initIV);
for (long j = 0; j < counter; j++) {
assertIVCalculation(codec, initIV, j, IV);
}
}
// Random IV and random counter test
for (long i = 0; i < iterations; i++) {
sr.nextBytes(initIV);
for (long j = 0; j < counter; j++) {
long c = sr.nextLong();
assertIVCalculation(codec, initIV, c, IV);
}
}
}
Aggregations