use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.
the class AbstractIdentityValidator method decryptTokenData.
/**
* Decrypt the data contained in a {@link UserNameIdentityToken} or {@link IssuedIdentityToken}.
* <p>
* See {@link UserNameIdentityToken#getPassword()} and {@link IssuedIdentityToken#getTokenData()}.
*
* @param session the current {@link Session}.
* @param dataBytes the encrypted data.
* @return the decrypted data.
* @throws UaException if decryption fails.
*/
protected byte[] decryptTokenData(Session session, SecurityAlgorithm algorithm, byte[] dataBytes) throws UaException {
X509Certificate certificate = CertificateUtil.decodeCertificate(session.getEndpoint().getServerCertificate().bytesOrEmpty());
int cipherTextBlockSize = SecureChannel.getAsymmetricCipherTextBlockSize(certificate, algorithm);
int blockCount = dataBytes.length / cipherTextBlockSize;
int plainTextBufferSize = cipherTextBlockSize * blockCount;
byte[] plainTextBytes = new byte[plainTextBufferSize];
ByteBuffer plainTextNioBuffer = ByteBuffer.wrap(plainTextBytes);
ByteBuffer passwordNioBuffer = ByteBuffer.wrap(dataBytes);
try {
KeyPair keyPair = session.getServer().getConfig().getCertificateManager().getKeyPair(ByteString.of(DigestUtil.sha1(certificate.getEncoded()))).orElseThrow(() -> new UaException(StatusCodes.Bad_SecurityChecksFailed));
Cipher cipher = getCipher(algorithm, keyPair);
for (int blockNumber = 0; blockNumber < blockCount; blockNumber++) {
((Buffer) passwordNioBuffer).limit(passwordNioBuffer.position() + cipherTextBlockSize);
cipher.doFinal(passwordNioBuffer, plainTextNioBuffer);
}
} catch (GeneralSecurityException e) {
throw new UaException(StatusCodes.Bad_SecurityChecksFailed, e);
}
return plainTextBytes;
}
use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.
the class ManagedAddressSpace method write.
@Override
public void write(WriteContext context, List<WriteValue> writeValues) {
List<StatusCode> results = Lists.newArrayListWithCapacity(writeValues.size());
for (WriteValue writeValue : writeValues) {
UaServerNode node = nodeManager.get(writeValue.getNodeId());
if (node != null) {
try {
node.writeAttribute(new AttributeContext(context), writeValue.getAttributeId(), writeValue.getValue(), writeValue.getIndexRange());
results.add(StatusCode.GOOD);
logger.debug("Wrote value {} to {} attribute of {}", writeValue.getValue().getValue(), AttributeId.from(writeValue.getAttributeId()).map(Object::toString).orElse("unknown"), node.getNodeId());
} catch (UaException e) {
logger.error("Unable to write value={}", writeValue.getValue(), e);
results.add(e.getStatusCode());
}
} else {
results.add(new StatusCode(StatusCodes.Bad_NodeIdUnknown));
}
}
context.success(results);
}
use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.
the class ManagedAddressSpace method getInvocationHandler.
/**
* Get the {@link MethodInvocationHandler} for the method identified by {@code methodId}.
*
* @param objectId the {@link NodeId} identifying the object the method will be invoked on.
* @param methodId the {@link NodeId} identifying the method.
* @return the {@link MethodInvocationHandler} for {@code methodId}.
* @throws UaException a {@link UaException} containing the appropriate operation result if
* either the object or method can't be found.
*/
protected MethodInvocationHandler getInvocationHandler(NodeId objectId, NodeId methodId) throws UaException {
UaNode node = nodeManager.getNode(objectId).orElseThrow(() -> new UaException(StatusCodes.Bad_NodeIdUnknown));
UaMethodNode methodNode = null;
if (node instanceof UaObjectNode) {
UaObjectNode objectNode = (UaObjectNode) node;
methodNode = objectNode.findMethodNode(methodId);
} else if (node instanceof UaObjectTypeNode) {
UaObjectTypeNode objectTypeNode = (UaObjectTypeNode) node;
methodNode = objectTypeNode.findMethodNode(methodId);
}
if (methodNode != null) {
return methodNode.getInvocationHandler();
} else {
throw new UaException(StatusCodes.Bad_MethodInvalid);
}
}
use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.
the class UaReferenceTypeNode method writeInverseName.
/**
* Write a new InverseName attribute for this Node to the server and update the local attribute
* if the operation succeeds.
*
* @param inverseName the {@link LocalizedText} to write to the server.
* @throws UaException if a service- or operation-level error occurs.
*/
public void writeInverseName(LocalizedText inverseName) throws UaException {
DataValue value = DataValue.valueOnly(new Variant(inverseName));
StatusCode statusCode = writeAttribute(AttributeId.InverseName, value);
if (statusCode != null && statusCode.isBad()) {
throw new UaException(statusCode, "write InverseName failed");
} else {
setInverseName(inverseName);
}
}
use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.
the class UaReferenceTypeNode method writeSymmetric.
/**
* Write a new Symmetric attribute for this Node to the server and update the local attribute
* if the operation succeeds.
*
* @param symmetric the {@link Boolean} to write to the server.
* @throws UaException if a service- or operation-level error occurs.
*/
public void writeSymmetric(Boolean symmetric) throws UaException {
DataValue value = DataValue.valueOnly(new Variant(symmetric));
StatusCode statusCode = writeAttribute(AttributeId.Symmetric, value);
if (statusCode != null && statusCode.isBad()) {
throw new UaException(statusCode, "write Symmetric failed");
} else {
setSymmetric(symmetric);
}
}
Aggregations