Skip to content
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

Better handle signature errors #686

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion opcua/common/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ def from_header_and_body(security_policy, header, buf):
if signature_size > 0:
signature = decrypted[-signature_size:]
decrypted = decrypted[:-signature_size]
crypto.verify(header_to_binary(obj.MessageHeader) + struct_to_binary(obj.SecurityHeader) + decrypted, signature)
try:
crypto.verify(header_to_binary(obj.MessageHeader) + struct_to_binary(obj.SecurityHeader) + decrypted, signature)
except UaError:
logger.exception("Could not verify signature for message {}".format(obj))
data = ua.utils.Buffer(crypto.remove_padding(decrypted))
obj.SequenceHeader = struct_from_binary(ua.SequenceHeader, data)
obj.Body = data.read(len(data))
Expand Down
2 changes: 1 addition & 1 deletion opcua/crypto/security_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def signature_size(self):
def verify(self, data, signature):
expected = uacrypto.hmac_sha1(self.key, data)
if signature != expected:
raise uacrypto.InvalidSignature
raise UaError("Invalid signature in data {} with signature {}".format(data, signature))


class EncryptorAesCbc(Encryptor):
Expand Down
1 change: 0 additions & 1 deletion opcua/crypto/uacrypto.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os

from cryptography import x509
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes
Expand Down