Search in sources :

Example 16 with UaException

use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.

the class OpcUaCertificateUsageChecker method check.

@Override
public void check(Certificate cert, Collection<String> unresolvedCritExts) throws CertPathValidatorException {
    X509Certificate certificate = (X509Certificate) cert;
    Set<String> criticalExtensions = certificate.getCriticalExtensionOIDs();
    if (criticalExtensions == null)
        criticalExtensions = Collections.emptySet();
    if (endEntityCert.equals(cert)) {
        try {
            CertificateValidationUtil.checkEndEntityKeyUsage((X509Certificate) cert);
            LOGGER.debug("validated KeyUsage for end entity: {}", ((X509Certificate) cert).getSubjectX500Principal().getName());
        } catch (UaException e) {
            if (validationChecks.contains(ValidationCheck.KEY_USAGE_END_ENTITY) || criticalExtensions.contains(KEY_USAGE_OID)) {
                throw new CertPathValidatorException(e.getMessage(), e, certPath, certPath.getCertificates().indexOf(cert), PKIXReason.INVALID_KEY_USAGE);
            } else {
                LOGGER.warn("check suppressed: certificate failed end-entity usage check: {}", ((X509Certificate) cert).getSubjectX500Principal().getName());
            }
        }
        try {
            CertificateValidationUtil.checkEndEntityExtendedKeyUsage(certificate, endEntityIsClient);
            LOGGER.debug("validated ExtendedKeyUsage for end entity: {}", ((X509Certificate) cert).getSubjectX500Principal().getName());
        } catch (UaException e) {
            if (validationChecks.contains(ValidationCheck.EXTENDED_KEY_USAGE_END_ENTITY) || criticalExtensions.contains(EXTENDED_KEY_USAGE_OID)) {
                throw new CertPathValidatorException(e.getMessage(), e, certPath, certPath.getCertificates().indexOf(cert), PKIXReason.INVALID_KEY_USAGE);
            } else {
                LOGGER.warn("check suppressed: certificate failed end-entity usage check: {}", ((X509Certificate) cert).getSubjectX500Principal().getName());
            }
        }
    } else {
        try {
            CertificateValidationUtil.checkIssuerKeyUsage((X509Certificate) cert);
            LOGGER.debug("validated KeyUsage for issuer: {}", ((X509Certificate) cert).getSubjectX500Principal().getName());
        } catch (UaException e) {
            if (validationChecks.contains(ValidationCheck.KEY_USAGE_ISSUER) || criticalExtensions.contains(KEY_USAGE_OID)) {
                throw new CertPathValidatorException(e.getMessage(), e, certPath, certPath.getCertificates().indexOf(cert), PKIXReason.INVALID_KEY_USAGE);
            } else {
                LOGGER.warn("check suppressed: certificate failed issuer usage check: {}", ((X509Certificate) cert).getSubjectX500Principal().getName());
            }
        }
    }
    if (unresolvedCritExts != null && !unresolvedCritExts.isEmpty()) {
        unresolvedCritExts.remove(KEY_USAGE_OID);
        unresolvedCritExts.remove(EXTENDED_KEY_USAGE_OID);
    }
}
Also used : CertPathValidatorException(java.security.cert.CertPathValidatorException) UaException(org.eclipse.milo.opcua.stack.core.UaException) X509Certificate(java.security.cert.X509Certificate)

Example 17 with UaException

use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.

the class AddressSpace method createObjectNodeFromBaseAttributes.

private CompletableFuture<UaObjectNode> createObjectNodeFromBaseAttributes(NodeId nodeId, List<DataValue> baseAttributeValues) {
    Set<AttributeId> remainingAttributes = Sets.difference(AttributeId.OBJECT_ATTRIBUTES, AttributeId.BASE_ATTRIBUTES);
    CompletableFuture<ReadResponse> attributesFuture = readAttributes(nodeId, remainingAttributes);
    CompletableFuture<NodeId> typeDefinitionFuture = readTypeDefinition(nodeId);
    return CompletableFuture.allOf(attributesFuture, typeDefinitionFuture).thenCompose(ignored -> {
        ReadResponse response = attributesFuture.join();
        NodeId typeDefinitionId = typeDefinitionFuture.join();
        List<DataValue> attributeValues = new ArrayList<>(baseAttributeValues);
        Collections.addAll(attributeValues, response.getResults());
        try {
            UaObjectNode node = newObjectNode(nodeId, typeDefinitionId, attributeValues);
            nodeCache.put(node.getNodeId(), node);
            return completedFuture(node);
        } catch (UaException e) {
            return failedFuture(e);
        }
    });
}
Also used : UaObjectNode(org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) UaException(org.eclipse.milo.opcua.stack.core.UaException) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) ArrayList(java.util.ArrayList)

Example 18 with UaException

use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.

the class AddressSpace method createViewNodeFromBaseAttributes.

private CompletableFuture<UaViewNode> createViewNodeFromBaseAttributes(NodeId nodeId, List<DataValue> baseAttributeValues) {
    Set<AttributeId> remainingAttributes = Sets.difference(AttributeId.VIEW_ATTRIBUTES, AttributeId.BASE_ATTRIBUTES);
    CompletableFuture<ReadResponse> attributesFuture = readAttributes(nodeId, remainingAttributes);
    return attributesFuture.thenCompose(response -> {
        List<DataValue> attributeValues = new ArrayList<>(baseAttributeValues);
        Collections.addAll(attributeValues, response.getResults());
        try {
            UaViewNode node = newViewNode(nodeId, attributeValues);
            nodeCache.put(node.getNodeId(), node);
            return completedFuture(node);
        } catch (UaException e) {
            return failedFuture(e);
        }
    });
}
Also used : ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) UaException(org.eclipse.milo.opcua.stack.core.UaException) ArrayList(java.util.ArrayList) UaViewNode(org.eclipse.milo.opcua.sdk.client.nodes.UaViewNode)

Example 19 with UaException

use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.

the class AddressSpace method getVariableNodeAsync.

/**
 * Get a {@link UaVariableNode} instance for the VariableNode identified by {@code nodeId},
 * assuming the type definition identified by {@code typeDefinitionId}.
 * <p>
 * If this type definition is registered with the {@link VariableTypeManager} a
 * {@link UaVariableNode} of the appropriate subclass will be returned.
 * <p>
 * This call completes asynchronously.
 *
 * @param nodeId           the {@link NodeId} identifying the VariableNode to get.
 * @param typeDefinitionId the {@link NodeId} identifying the type definition.
 * @return a CompletableFuture that completes successfully with a {@link UaVariableNode}
 * instance for the VariableNode identified by {@code nodeId} or completes exceptionally if an
 * error occurs while creating the VariableNode.
 */
public CompletableFuture<UaVariableNode> getVariableNodeAsync(NodeId nodeId, NodeId typeDefinitionId) {
    UaNode cachedNode = nodeCache.getIfPresent(nodeId);
    if (cachedNode instanceof UaVariableNode) {
        return completedFuture((UaVariableNode) cachedNode);
    } else {
        CompletableFuture<ReadResponse> future = readAttributes(nodeId, AttributeId.VARIABLE_ATTRIBUTES);
        return future.thenCompose(response -> {
            List<DataValue> attributeValues = l(response.getResults());
            try {
                UaVariableNode node = newVariableNode(nodeId, typeDefinitionId, attributeValues);
                nodeCache.put(node.getNodeId(), node);
                return completedFuture(node);
            } catch (UaException e) {
                return failedFuture(e);
            }
        });
    }
}
Also used : UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaException(org.eclipse.milo.opcua.stack.core.UaException) UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode)

Example 20 with UaException

use of org.eclipse.milo.opcua.stack.core.UaException in project milo by eclipse.

the class AddressSpace method newMethodNode.

private UaMethodNode newMethodNode(NodeId nodeId, List<DataValue> attributeValues) throws UaException {
    DataValue nodeIdDataValue = attributeValues.get(0);
    StatusCode nodeIdStatusCode = nodeIdDataValue.getStatusCode();
    if (nodeIdStatusCode != null && nodeIdStatusCode.isBad()) {
        throw new UaException(nodeIdStatusCode);
    }
    try {
        NodeClass nodeClass = NodeClass.from((Integer) attributeValues.get(1).getValue().getValue());
        Preconditions.checkArgument(nodeClass == NodeClass.Method, "expected NodeClass.Method, got NodeClass." + nodeClass);
        QualifiedName browseName = (QualifiedName) attributeValues.get(2).getValue().getValue();
        LocalizedText displayName = (LocalizedText) attributeValues.get(3).getValue().getValue();
        LocalizedText description = getAttributeOrNull(attributeValues.get(4), LocalizedText.class);
        UInteger writeMask = getAttributeOrNull(attributeValues.get(5), UInteger.class);
        UInteger userWriteMask = getAttributeOrNull(attributeValues.get(6), UInteger.class);
        Boolean executable = (Boolean) attributeValues.get(7).getValue().getValue();
        Boolean userExecutable = (Boolean) attributeValues.get(8).getValue().getValue();
        return new UaMethodNode(client, nodeId, nodeClass, browseName, displayName, description, writeMask, userWriteMask, executable, userExecutable);
    } catch (Throwable t) {
        throw UaException.extract(t).orElse(new UaException(StatusCodes.Bad_UnexpectedError, t));
    }
}
Also used : NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) UaMethodNode(org.eclipse.milo.opcua.sdk.client.nodes.UaMethodNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaException(org.eclipse.milo.opcua.stack.core.UaException) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText)

Aggregations

UaException (org.eclipse.milo.opcua.stack.core.UaException)213 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)99 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)94 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)53 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)49 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)37 List (java.util.List)28 StatusCodes (org.eclipse.milo.opcua.stack.core.StatusCodes)27 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)27 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)27 ArrayList (java.util.ArrayList)26 X509Certificate (java.security.cert.X509Certificate)24 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)23 SecurityPolicy (org.eclipse.milo.opcua.stack.core.security.SecurityPolicy)21 AttributeId (org.eclipse.milo.opcua.stack.core.AttributeId)19 LocalizedText (org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText)19 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)17 NodeClass (org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass)17 LoggerFactory (org.slf4j.LoggerFactory)17 Logger (org.slf4j.Logger)16