Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def encrypted_size(self, plain_size):
size = plain_size + self.security_policy.signature_size()
pbs = self.security_policy.plain_block_size()
if size % pbs != 0:
print("ENC", plain_size, size, pbs)
raise ua.UaError("Encryption error")
return size // pbs * self.security_policy.encrypted_block_size()
def test_basic256sha56_encrypt_fail(self):
# FIXME: how to make it fail???
clt = Client(self.uri_crypto)
with self.assertRaises(ua.UaError):
clt.set_security(security_policies.SecurityPolicyBasic256Sha256,
'examples/certificate-example.der',
'examples/private-key-example.pem',
None,
ua.MessageSecurityMode.None_
)
def test_add_node_bad_args(self):
obj = self.opc.get_objects_node()
with self.assertRaises(TypeError):
fold = obj.add_folder(1.2, "kk")
with self.assertRaises(TypeError):
fold = obj.add_folder(ua.UaError, "khjh")
with self.assertRaises(ua.UaError):
fold = obj.add_folder("kjk", 1.2)
with self.assertRaises(TypeError):
fold = obj.add_folder("i=0;s='oooo'", 1.2)
with self.assertRaises(ua.UaError):
fold = obj.add_folder("i=0;s='oooo'", "tt:oioi")
if self._incoming_parts[0].SequenceHeader.RequestId != chunk.SequenceHeader.RequestId:
raise ua.UaError("Wrong request id {0}, expected {1}".format(
chunk.SequenceHeader.RequestId,
self._incoming_parts[0].SequenceHeader.RequestId))
# sequence number must be incremented or wrapped
num = chunk.SequenceHeader.SequenceNumber
if self._peer_sequence_number is not None:
if num != self._peer_sequence_number + 1:
wrap = (1 << 32) - 1024
if num < 1024 and self._peer_sequence_number >= wrap:
# specs Part 6, 6.7.2
logger.debug("Sequence number wrapped: %d -> %d",
self._peer_sequence_number, num)
else:
raise ua.UaError(
"Wrong sequence {0} -> {1} (server bug or replay attack)"
.format(self._peer_sequence_number, num))
self._peer_sequence_number = num
def historize_data_change(self, node, period=timedelta(days=7), count=0):
"""
Subscribe to the nodes' data changes and store the data in the active storage.
"""
if not self._sub:
self._sub = self._create_subscription(SubHandler(self.storage))
if node in self._handlers:
raise ua.UaError("Node {0} is already historized".format(node))
self.storage.new_historized_node(node.nodeid, period, count)
handler = self._sub.subscribe_data_change(node)
self._handlers[node] = handler
params.SessionName = self.description + " Session" + str(self._session_counter)
# START MODIFICATION
params.RequestedSessionTimeout = self.session_timeout
# END MODIFICATION
params.MaxResponseMessageSize = 0 # means no max size
response = self.uaclient.create_session(params)
if self.security_policy.client_certificate is None:
data = nonce
else:
data = self.security_policy.client_certificate + nonce
self.security_policy.asymmetric_cryptography.verify(data, response.ServerSignature.Signature)
self._server_nonce = response.ServerNonce
if not self.security_policy.server_certificate:
self.security_policy.server_certificate = response.ServerCertificate
elif self.security_policy.server_certificate != response.ServerCertificate:
raise ua.UaError("Server certificate mismatch")
# remember PolicyId's: we will use them in activate_session()
ep = self.find_endpoint(response.ServerEndpoints, self.security_policy.Mode, self.security_policy.URI)
self._policy_ids = ep.UserIdentityTokens
self.session_timeout = response.RevisedSessionTimeout
# START MODIFICATION
## remove keep alive
# END MODIFICATION
return response
def _call_callback(self, request_id, body):
with self._lock:
future = self._callbackmap.pop(request_id, None)
if future is None:
raise ua.UaError(
"No future object found for request: {0}, callbacks in list are {1}"
.format(request_id, self._callbackmap.keys())
)
future.set_result(body)
params.ClientDescription = desc
params.EndpointUrl = self.server_url.geturl()
params.SessionName = self.description + " Session" + str(self._session_counter)
params.RequestedSessionTimeout = 3600000
params.MaxResponseMessageSize = 0 # means no max size
response = self.uaclient.create_session(params)
if self.security_policy.client_certificate is None:
data = nonce
else:
data = self.security_policy.client_certificate + nonce
self.security_policy.asymmetric_cryptography.verify(data, response.ServerSignature.Signature)
self._server_nonce = response.ServerNonce
if not self.security_policy.server_certificate:
self.security_policy.server_certificate = response.ServerCertificate
elif self.security_policy.server_certificate != response.ServerCertificate:
raise ua.UaError("Server certificate mismatch")
# remember PolicyId's: we will use them in activate_session()
ep = Client.find_endpoint(response.ServerEndpoints, self.security_policy.Mode, self.security_policy.URI)
self._policy_ids = ep.UserIdentityTokens
self.session_timeout = response.RevisedSessionTimeout
self.keepalive = KeepAlive(
self, min(self.session_timeout, self.secure_channel_timeout) * 0.7) # 0.7 is from spec
self.keepalive.start()
return response
def _show_structs(self):
base_struct = self.server_mgr.get_node(ua.ObjectIds.Structure)
opc_binary = self.server_mgr.get_node(ua.ObjectIds.OPCBinarySchema_TypeSystem)
opc_schema = self.server_mgr.get_node(ua.ObjectIds.OpcUa_BinarySchema)
for node in opc_binary.get_children():
if node == opc_schema:
continue # This is standard namespace structures
try:
ns = node.get_child("0:NamespaceUri").get_value()
ar = self.server_mgr.get_namespace_array()
idx = ar.index(ns)
except ua.UaError:
idx = 1
xml = node.get_value()
if not xml:
return
xml = xml.decode("utf-8")
generator = StructGenerator()
generator.make_model_from_string(xml)
for el in generator.model:
# we only care about structs, ignoring enums
if isinstance(el, Struct):
self._add_design_node(base_struct, idx, el)
def select_policy(self, uri, peer_certificate, mode=None):
for policy in self._policies:
if policy.matches(uri, mode):
self.security_policy = policy.create(peer_certificate)
return
if self.security_policy.URI != uri or (mode is not None and
self.security_policy.Mode != mode):
raise ua.UaError("No matching policy: {0}, {1}".format(uri, mode))