Skip to content

Commit

Permalink
Revert commits breaking API and binary OCPP 1.6 passwords
Browse files Browse the repository at this point in the history
These commits were based on a misunderstanding of the OCPP-J 1.6
specification, which clearly states that the password is a byte sequence
and not a string:

Revert "More password fixes."

This reverts commit fb5d0fb.

Revert "Fix password decoding."

This reverts commit cf20205.

Revert "Recommended by 1.6 spec is a 20 byte (40 chars) key."

This reverts commit f7b92a3.

This commit breaks the API, because the behaviour of the method is
changed to return the last configuration instead of the default
configuration:

Revert "A single instace, otherwise a static get() method makes no sense."

This reverts commit 953f50b.
  • Loading branch information
robert-s-ubi committed Jun 28, 2024
1 parent 088b78c commit 63fe226
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ public class JSONConfiguration {

private JSONConfiguration() {}

private static final JSONConfiguration instance = new JSONConfiguration();

public static JSONConfiguration get() {
return instance;
return new JSONConfiguration();
}

public <T> JSONConfiguration setParameter(String name, T value) {
Expand Down
10 changes: 5 additions & 5 deletions OCPP-J/src/main/java/eu/chargetime/ocpp/WebSocketListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class WebSocketListener implements Listener {
private static final int TIMEOUT_IN_MILLIS = 10000;

private static final int OCPPJ_CP_MIN_PASSWORD_LENGTH = 16;
private static final int OCPPJ_CP_MAX_PASSWORD_LENGTH = 40;
private static final int OCPPJ_CP_MAX_PASSWORD_LENGTH = 20;

private static final String HTTP_HEADER_PROXIED_ADDRESS = "X-Forwarded-For";

Expand Down Expand Up @@ -146,7 +146,7 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(
.build();

String username = null;
String password = null;
byte[] password = null;
if (clientHandshake.hasFieldValue("Authorization")) {
String authorization = clientHandshake.getFieldValue("Authorization");
if (authorization != null && authorization.toLowerCase().startsWith("basic")) {
Expand All @@ -159,15 +159,15 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(
username =
new String(Arrays.copyOfRange(credDecoded, 0, i), StandardCharsets.UTF_8);
if (i + 1 < credDecoded.length) {
password = new String(Arrays.copyOfRange(credDecoded, i + 1, credDecoded.length));
password = Arrays.copyOfRange(credDecoded, i + 1, credDecoded.length);
}
break;
}
}
}
if (password == null
|| password.length() < configuration.getParameter(JSONConfiguration.OCPPJ_CP_MIN_PASSWORD_LENGTH, OCPPJ_CP_MIN_PASSWORD_LENGTH)
|| password.length() > configuration.getParameter(JSONConfiguration.OCPPJ_CP_MAX_PASSWORD_LENGTH, OCPPJ_CP_MAX_PASSWORD_LENGTH))
|| password.length < configuration.getParameter(JSONConfiguration.OCPPJ_CP_MIN_PASSWORD_LENGTH, OCPPJ_CP_MIN_PASSWORD_LENGTH)
|| password.length > configuration.getParameter(JSONConfiguration.OCPPJ_CP_MAX_PASSWORD_LENGTH, OCPPJ_CP_MAX_PASSWORD_LENGTH))
throw new InvalidDataException(401, "Invalid password length");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ of this software and associated documentation files (the "Software"), to deal
import eu.chargetime.ocpp.model.SessionInformation;

public interface ListenerEvents {
void authenticateSession(SessionInformation information, String username, String password)
void authenticateSession(SessionInformation information, String username, byte[] password)
throws AuthenticationException;

void newSession(ISession session, SessionInformation information);
Expand Down
2 changes: 1 addition & 1 deletion ocpp-common/src/main/java/eu/chargetime/ocpp/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void open(String hostname, int port, ServerEvents serverEvents) {

@Override
public void authenticateSession(
SessionInformation information, String username, String password)
SessionInformation information, String username, byte[] password)
throws AuthenticationException {
serverEvents.authenticateSession(information, username, password);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal
import java.util.UUID;

public interface ServerEvents {
void authenticateSession(SessionInformation information, String username, String password) throws AuthenticationException;
void authenticateSession(SessionInformation information, String username, byte[] password) throws AuthenticationException;

void newSession(UUID sessionIndex, SessionInformation information);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public ServerEvents generateServerEventsHandler() {
return new ServerEvents() {
@Override
public void authenticateSession(
SessionInformation information, String username, String password) throws AuthenticationException {}
SessionInformation information, String username, byte[] password) throws AuthenticationException {}

@Override
public void newSession(UUID sessionIndex, SessionInformation information) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(
.build();

String username = null;
String password = null;
byte[] password = null;
if (clientHandshake.hasFieldValue("Authorization")) {
String authorization = clientHandshake.getFieldValue("Authorization");
if (authorization != null && authorization.toLowerCase().startsWith("basic")) {
Expand All @@ -178,21 +178,21 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(
username =
new String(Arrays.copyOfRange(credDecoded, 0, i), StandardCharsets.UTF_8);
if (i + 1 < credDecoded.length) {
password = new String(Arrays.copyOfRange(credDecoded, i + 1, credDecoded.length));
password = Arrays.copyOfRange(credDecoded, i + 1, credDecoded.length);
}
break;
}
}
}
if (protocolVersion == null || protocolVersion == ProtocolVersion.OCPP1_6) {
if (password == null
|| password.length() < configuration.getParameter(JSONConfiguration.OCPPJ_CP_MIN_PASSWORD_LENGTH, OCPPJ_CP_MIN_PASSWORD_LENGTH)
|| password.length() > configuration.getParameter(JSONConfiguration.OCPPJ_CP_MAX_PASSWORD_LENGTH, OCPPJ_CP_MAX_PASSWORD_LENGTH))
|| password.length < configuration.getParameter(JSONConfiguration.OCPPJ_CP_MIN_PASSWORD_LENGTH, OCPPJ_CP_MIN_PASSWORD_LENGTH)
|| password.length > configuration.getParameter(JSONConfiguration.OCPPJ_CP_MAX_PASSWORD_LENGTH, OCPPJ_CP_MAX_PASSWORD_LENGTH))
throw new InvalidDataException(401, "Invalid password length");
} else {
if (password == null
|| password.length() < configuration.getParameter(JSONConfiguration.OCPP2J_CP_MIN_PASSWORD_LENGTH, OCPP2J_CP_MIN_PASSWORD_LENGTH)
|| password.length() > configuration.getParameter(JSONConfiguration.OCPP2J_CP_MAX_PASSWORD_LENGTH, OCPP2J_CP_MAX_PASSWORD_LENGTH))
|| password.length < configuration.getParameter(JSONConfiguration.OCPP2J_CP_MIN_PASSWORD_LENGTH, OCPP2J_CP_MIN_PASSWORD_LENGTH)
|| password.length > configuration.getParameter(JSONConfiguration.OCPP2J_CP_MAX_PASSWORD_LENGTH, OCPP2J_CP_MAX_PASSWORD_LENGTH))
throw new InvalidDataException(401, "Invalid password length");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void started() throws Exception {
new ServerEvents() {
@Override
public void authenticateSession(
SessionInformation information, String username, String password) throws AuthenticationException {}
SessionInformation information, String username, byte[] password) throws AuthenticationException {}

@Override
public void newSession(UUID sessionIndex, SessionInformation information) {
Expand Down

0 comments on commit 63fe226

Please sign in to comment.