Skip to content

Commit

Permalink
Merge pull request #860 from AlinMoldovean/master
Browse files Browse the repository at this point in the history
Nonce validation fix in Client Session
  • Loading branch information
AlinMoldovean authored Dec 2, 2019
2 parents de98723 + 77a183e commit f1a16df
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion SampleApplications/SDK/Opc.Ua.Client/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ private void ValidateServerNonce(IUserIdentity identity, byte[] serverNonce, str
if (identity!= null && identity.TokenType != UserTokenType.Anonymous)
{
// the server nonce should be validated if the token includes a secret.
if (!Utils.Nonce.ValidateNonce(serverNonce, MessageSecurityMode.SignAndEncrypt, securityPolicyUri))
if (!Utils.Nonce.ValidateNonce(serverNonce, MessageSecurityMode.SignAndEncrypt, (uint)m_configuration.SecurityConfiguration.NonceLength))
{
throw ServiceResultException.Create(StatusCodes.BadNonceInvalid, "Server nonce is not the correct length or not random enough.");
}
Expand Down
10 changes: 9 additions & 1 deletion Stack/Opc.Ua.Core/Types/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2664,6 +2664,14 @@ public static uint GetNonceLength(string securityPolicyUri)
/// Validates the nonce for a message security mode and security policy.
/// </summary>
public static bool ValidateNonce(byte[] nonce, MessageSecurityMode securityMode, string securityPolicyUri)
{
return ValidateNonce(nonce, securityMode, GetNonceLength(securityPolicyUri));
}

/// <summary>
/// Validates the nonce for a message security mode and a minimum length.
/// </summary>
public static bool ValidateNonce(byte[] nonce, MessageSecurityMode securityMode, uint minNonceLength)
{
// no nonce needed for no security.
if (securityMode == MessageSecurityMode.None)
Expand All @@ -2672,7 +2680,7 @@ public static bool ValidateNonce(byte[] nonce, MessageSecurityMode securityMode,
}

// check the length.
if (nonce == null || nonce.Length < GetNonceLength(securityPolicyUri))
if (nonce == null || nonce.Length < minNonceLength)
{
return false;
}
Expand Down

0 comments on commit f1a16df

Please sign in to comment.