Skip to content

Commit d9d89aa

Browse files
authored
Allow configuration of a max password length and default to 1024 (#568)
1 parent a3a3e1f commit d9d89aa

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

build-tools/src/main/resources/milo/checkstyle.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,5 +215,9 @@
215215
<property name="exceptionVariableName" value="expected"/>
216216
</module>
217217
<module name="CommentsIndentation"/>
218+
<module name="SuppressionCommentFilter">
219+
<property name="offCommentFormat" value="@formatter\:off"/>
220+
<property name="onCommentFormat" value="@formatter\:on"/>
221+
</module>
218222
</module>
219223
</module>

opc-ua-sdk/sdk-server/src/main/java/org/eclipse/milo/opcua/sdk/server/api/config/OpcUaServerConfigLimits.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ default Double getMaxSupportedSampleRate() {
8383
return (double) TimeUnit.MILLISECONDS.convert(24, TimeUnit.HOURS);
8484
}
8585

86+
/**
87+
* Get the maximum allowed password length.
88+
*
89+
* @return the maximum allowed password length.
90+
*/
91+
default UInteger getMaxPasswordLength() {
92+
return uint(1024);
93+
}
94+
8695
//region ServerCapabilities
8796

8897
default Double getMinSupportedSampleRate() {

opc-ua-sdk/sdk-server/src/main/java/org/eclipse/milo/opcua/sdk/server/identity/UsernameIdentityValidator.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,29 @@ private String validateUserNameIdentityToken(
103103
if (algorithm != SecurityAlgorithm.None) {
104104
byte[] plainTextBytes = decryptTokenData(session, algorithm, tokenBytes);
105105

106-
int length = ((plainTextBytes[3] & 0xFF) << 24) |
107-
((plainTextBytes[2] & 0xFF) << 16) |
108-
((plainTextBytes[1] & 0xFF) << 8) |
109-
(plainTextBytes[0] & 0xFF);
106+
//@formatter:off
107+
long length =
108+
((plainTextBytes[3] & 0xFFL) << 24) |
109+
((plainTextBytes[2] & 0xFFL) << 16) |
110+
((plainTextBytes[1] & 0xFFL) << 8) |
111+
( plainTextBytes[0] & 0xFFL );
112+
//@formatter:on
113+
114+
if (length != plainTextBytes.length - 4) {
115+
throw new UaException(StatusCodes.Bad_IdentityTokenInvalid, "invalid token data");
116+
}
117+
118+
int passwordLength = (int) length - lastNonceLength;
119+
120+
if (passwordLength < 0) {
121+
throw new UaException(StatusCodes.Bad_IdentityTokenInvalid, "invalid password length");
122+
}
123+
124+
if (passwordLength > session.getServer().getConfig().getLimits().getMaxPasswordLength().longValue()) {
125+
throw new UaException(StatusCodes.Bad_EncodingLimitsExceeded, "password length exceeds limits");
126+
}
110127

111-
byte[] passwordBytes = new byte[length - lastNonceLength];
128+
byte[] passwordBytes = new byte[passwordLength];
112129
byte[] nonceBytes = new byte[lastNonceLength];
113130

114131
System.arraycopy(plainTextBytes, 4, passwordBytes, 0, passwordBytes.length);

0 commit comments

Comments
 (0)