Search in sources :

Example 26 with UaException

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;
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(java.nio.Buffer) KeyPair(java.security.KeyPair) UaException(org.eclipse.milo.opcua.stack.core.UaException) GeneralSecurityException(java.security.GeneralSecurityException) Cipher(javax.crypto.Cipher) ByteBuffer(java.nio.ByteBuffer) X509Certificate(java.security.cert.X509Certificate)

Example 27 with UaException

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);
}
Also used : WriteValue(org.eclipse.milo.opcua.stack.core.types.structured.WriteValue) AttributeContext(org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext) UaException(org.eclipse.milo.opcua.stack.core.UaException) UaServerNode(org.eclipse.milo.opcua.sdk.server.nodes.UaServerNode) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)

Example 28 with UaException

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);
    }
}
Also used : UaObjectTypeNode(org.eclipse.milo.opcua.sdk.server.nodes.UaObjectTypeNode) UaMethodNode(org.eclipse.milo.opcua.sdk.server.nodes.UaMethodNode) UaObjectNode(org.eclipse.milo.opcua.sdk.server.nodes.UaObjectNode) UaException(org.eclipse.milo.opcua.stack.core.UaException) UaNode(org.eclipse.milo.opcua.sdk.server.nodes.UaNode)

Example 29 with UaException

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);
    }
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaException(org.eclipse.milo.opcua.stack.core.UaException) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)

Example 30 with UaException

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);
    }
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaException(org.eclipse.milo.opcua.stack.core.UaException) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)

Aggregations

UaException (org.eclipse.milo.opcua.stack.core.UaException)223 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)102 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)97 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)56 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)51 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)41 List (java.util.List)29 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)29 ArrayList (java.util.ArrayList)28 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)28 StatusCodes (org.eclipse.milo.opcua.stack.core.StatusCodes)27 X509Certificate (java.security.cert.X509Certificate)25 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)25 SecurityPolicy (org.eclipse.milo.opcua.stack.core.security.SecurityPolicy)22 AttributeId (org.eclipse.milo.opcua.stack.core.AttributeId)20 LocalizedText (org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText)20 NodeClass (org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass)18 LoggerFactory (org.slf4j.LoggerFactory)18 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)17 Logger (org.slf4j.Logger)17