Skip to content

Commit

Permalink
Unified UserIdentityToken Encrypt/Decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsuciu committed Aug 30, 2024
1 parent f6f6ca3 commit d79719f
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 34 deletions.
20 changes: 3 additions & 17 deletions Libraries/Opc.Ua.Client/Session/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2456,7 +2456,6 @@ public void Open(
SignatureData userTokenSignature = identityToken.Sign(dataToSign, securityPolicyUri);

// encrypt token.
#if ECC_SUPPORT
identityToken.Encrypt(
serverCertificate,
serverNonce,
Expand All @@ -2465,9 +2464,7 @@ public void Open(
m_instanceCertificate,
m_instanceCertificateChain,
m_endpoint.Description.SecurityMode != MessageSecurityMode.None);
#else
identityToken.Encrypt(serverCertificate, serverNonce, securityPolicyUri);
#endif

// send the software certificates assigned to the client.
SignedSoftwareCertificateCollection clientSoftwareCertificates = GetSoftwareCertificates();

Expand Down Expand Up @@ -2641,8 +2638,6 @@ public void UpdateSession(IUserIdentity identity, StringCollection preferredLoca
userTokenSignature = identityToken.Sign(dataToSign, securityPolicyUri);

// encrypt token.
#if ECC_SUPPORT
// TODO: build helper function
identityToken.Encrypt(
m_serverCertificate,
serverNonce,
Expand All @@ -2651,9 +2646,7 @@ public void UpdateSession(IUserIdentity identity, StringCollection preferredLoca
m_instanceCertificate,
m_instanceCertificateChain,
m_endpoint.Description.SecurityMode != MessageSecurityMode.None);
#else
identityToken.Encrypt(m_serverCertificate, serverNonce, securityPolicyUri);
#endif

// send the software certificates assigned to the client.
SignedSoftwareCertificateCollection clientSoftwareCertificates = GetSoftwareCertificates();

Expand Down Expand Up @@ -5679,7 +5672,6 @@ ITransportChannel transportChannel
SignatureData userTokenSignature = identityToken.Sign(dataToSign, securityPolicyUri);

// encrypt token.
#if ECC_SUPPORT
identityToken.Encrypt(
m_serverCertificate,
m_serverNonce,
Expand All @@ -5688,9 +5680,6 @@ ITransportChannel transportChannel
m_instanceCertificate,
m_instanceCertificateChain,
m_endpoint.Description.SecurityMode != MessageSecurityMode.None);
#else
identityToken.Encrypt(m_serverCertificate, m_serverNonce, securityPolicyUri);
#endif

// send the software certificates assigned to the client.
SignedSoftwareCertificateCollection clientSoftwareCertificates = GetSoftwareCertificates();
Expand Down Expand Up @@ -6481,9 +6470,8 @@ protected virtual void ProcessResponseAdditionalHeader(ResponseHeader responseHe
StatusCodes.BadDecodingError,
"Could not verify signature on ECDHKey. User authentication not possible.");
}
#if ECC_SUPPORT

m_eccServerEphemeralKey = Nonce.CreateNonce(m_userTokenSecurityPolicyUri, key.PublicKey);
#endif
}
}
}
Expand Down Expand Up @@ -6579,9 +6567,7 @@ protected virtual void ProcessResponseAdditionalHeader(ResponseHeader responseHe
private int m_maxPublishRequestCount;
private LinkedList<AsyncRequestState> m_outstandingRequests;
private string m_userTokenSecurityPolicyUri;
#if ECC_SUPPORT
private Nonce m_eccServerEphemeralKey;
#endif
private readonly EndpointDescriptionCollection m_discoveryServerEndpoints;
private readonly StringCollection m_discoveryProfileUris;
private uint m_serverMaxContinuationPointsPerBrowse = 0;
Expand Down
5 changes: 0 additions & 5 deletions Libraries/Opc.Ua.Client/Session/SessionAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ public async Task OpenAsync(
SignatureData userTokenSignature = identityToken.Sign(dataToSign, securityPolicyUri);

// encrypt token.
//identityToken.Encrypt(serverCertificate, serverNonce, securityPolicyUri);
#if ECC_SUPPORT
identityToken.Encrypt(
serverCertificate,
serverNonce,
Expand All @@ -249,9 +247,6 @@ public async Task OpenAsync(
m_instanceCertificate,
m_instanceCertificateChain,
m_endpoint.Description.SecurityMode != MessageSecurityMode.None);
#else
identityToken.Encrypt(serverCertificate, serverNonce, securityPolicyUri);
#endif

// send the software certificates assigned to the client.
SignedSoftwareCertificateCollection clientSoftwareCertificates = GetSoftwareCertificates();
Expand Down
12 changes: 0 additions & 12 deletions Stack/Opc.Ua.Core/Stack/Types/UserIdentityToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,15 @@ public partial class UserIdentityToken
/// <summary>
/// Encrypts the token (implemented by the subclass).
/// </summary>
#if ECC_SUPPORT
[Obsolete("Use Encrypt(X509Certificate2, byte[], string securityPolicyUri, Nonce, X509Certificate2, X509Certificate2Collection, bool) ")]
#endif
public virtual void Encrypt(X509Certificate2 certificate, byte[] receiverNonce, string securityPolicyUri)
{
}

/// <summary>
/// Decrypts the token (implemented by the subclass).
/// </summary>
#if ECC_SUPPORT
[Obsolete("Use Decrypt(X509Certificate2, Nonce, string, Nonce, X509Certificate2, X509Certificate2Collection, CertificateValidator) ")]
#endif
public virtual void Decrypt(X509Certificate2 certificate, byte[] receiverNonce, string securityPolicyUri)
{
}
Expand Down Expand Up @@ -109,9 +105,7 @@ public string DecryptedPassword
/// <summary>
/// Encrypts the DecryptedPassword using the EncryptionAlgorithm and places the result in Password
/// </summary>
#if ECC_SUPPORT
[Obsolete("Use Encrypt(X509Certificate2, byte[], string securityPolicyUri, Nonce, X509Certificate2, X509Certificate2Collection, bool)")]
#endif
public override void Encrypt(X509Certificate2 certificate, byte[] senderNonce, string securityPolicyUri)
{
Encrypt(certificate, senderNonce, securityPolicyUri, null);
Expand All @@ -120,9 +114,7 @@ public override void Encrypt(X509Certificate2 certificate, byte[] senderNonce, s
/// <summary>
/// Decrypts the Password using the EncryptionAlgorithm and places the result in DecryptedPassword
/// </summary>
#if ECC_SUPPORT
[Obsolete("Use Decrypt(X509Certificate2, Nonce, string, Nonce, X509Certificate2, X509Certificate2Collection, CertificateValidator) ")]
#endif
public override void Decrypt(X509Certificate2 certificate, byte[] senderNonce, string securityPolicyUri)
{
Decrypt(certificate, Nonce.CreateNonce(securityPolicyUri, senderNonce), securityPolicyUri);
Expand Down Expand Up @@ -428,9 +420,7 @@ public byte[] DecryptedTokenData
/// <summary>
/// Encrypts the DecryptedTokenData using the EncryptionAlgorithm and places the result in Password
/// </summary>
#if ECC_SUPPORT
[Obsolete("Use Encrypt(X509Certificate2, byte[], string securityPolicyUri, Nonce, X509Certificate2, X509Certificate2Collection, bool)")]
#endif
public override void Encrypt(X509Certificate2 certificate, byte[] senderNonce, string securityPolicyUri)
{
Encrypt(certificate, senderNonce, securityPolicyUri, null);
Expand All @@ -439,9 +429,7 @@ public override void Encrypt(X509Certificate2 certificate, byte[] senderNonce, s
/// <summary>
/// Decrypts the Password using the EncryptionAlgorithm and places the result in DecryptedPassword
/// </summary>
#if ECC_SUPPORT
[Obsolete("Use Decrypt(X509Certificate2, Nonce, string, Nonce, X509Certificate2, X509Certificate2Collection, CertificateValidator) ")]
#endif
public override void Decrypt(X509Certificate2 certificate, byte[] senderNonce, string securityPolicyUri)
{
Decrypt(certificate, Nonce.CreateNonce(securityPolicyUri, senderNonce), securityPolicyUri);
Expand Down

0 comments on commit d79719f

Please sign in to comment.