8000 fix(webserver): use nonblocking SecureRandom (backport #115) by mergify[bot] · Pull Request #116 · cryostatio/cryostat-agent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(webserver): use nonblocking SecureRandom (backport #115) #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/main/java/io/cryostat/agent/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ public boolean checkCredentials(String username, String password) {
static class Credentials {

private static final String user = "agent";
private final SecureRandom random = new SecureRandom();
private byte[] passHash = new byte[0];
private byte[] pass = new byte[0];

Expand All @@ -307,32 +308,31 @@ synchronized boolean checkUserInfo(String username, String password)

synchronized void regenerate() throws NoSuchAlgorithmException {
this.clear();
final SecureRandom r = SecureRandom.getInstanceStrong();
final int len = 24;

this.pass = new byte[len];

// guarantee at least one character from each class
this.pass[0] = randomSymbol();
this.pass[1] = randomNumeric();
this.pass[2] = randomAlphabetical(r.nextBoolean());
this.pass[2] = randomAlphabetical(random.nextBoolean());

// fill remaining slots with randomly assigned characters across classes
for (int i = 3; i < len; i++) {
int s = r.nextInt(3);
int s = random.nextInt(3);
if (s == 0) {
this.pass[i] = randomSymbol();
} else if (s == 1) {
this.pass[i] = randomNumeric();
} else {
this.pass[i] = randomAlphabetical(r.nextBoolean());
this.pass[i] = randomAlphabetical(random.nextBoolean());
}
}

// randomly shuffle the characters
// https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
for (int i = this.pass.length - 1; i > 1; i--) {
int j = r.nextInt(i);
int j = random.nextInt(i);
byte b = this.pass[i];
this.pass[i] = this.pass[j];
this.pass[j] = b;
Expand All @@ -353,20 +353,20 @@ synchronized void clear() {
Arrays.fill(this.pass, (byte) 0);
}

private static byte randomAlphabetical(boolean upperCase) throws NoSuchAlgorithmException {
private byte randomAlphabetical(boolean upperCase) throws NoSuchAlgorithmException {
return randomChar(upperCase ? 'A' : 'a', 26);
}

private static byte randomNumeric() throws NoSuchAlgorithmException {
private byte randomNumeric() throws NoSuchAlgorithmException {
return randomChar('0', 10);
}

private static byte randomSymbol() throws NoSuchAlgorithmException {
private byte randomSymbol() throws NoSuchAlgorithmException {
return randomChar(33, 14);
}

private static byte randomChar(int offset, int range) throws NoSuchAlgorithmException {
return (byte) (SecureRandom.getInstanceStrong().nextInt(range) + offset);
private byte randomChar(int offset, int range) throws NoSuchAlgorithmException {
return (byte) (random.nextInt(range) + offset);
}

private static byte[] hash(String pass) throws NoSuchAlgorithmException {
Expand Down
0