Search in sources :

Example 6 with WonMessageProcessingException

use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.

the class SignatureAddingWonMessageProcessor method signWithDefaultKey.

/**
 * Used by the WoN node. Uses its default key for signing any outgoing message.
 *
 * @param message
 * @return
 * @throws WonMessageProcessingException
 */
public WonMessage signWithDefaultKey(final WonMessage message) throws WonMessageProcessingException {
    // use default key for signing
    PrivateKey privateKey = cryptographyService.getDefaultPrivateKey();
    String webId = cryptographyService.getDefaultPrivateKeyAlias();
    PublicKey publicKey = cryptographyService.getPublicKey(webId);
    try {
        List<WonMessage> ret = new ArrayList<WonMessage>();
        for (WonMessage part : message.getAllMessages()) {
            ret.add(processWithKey(part, webId, privateKey, publicKey));
        }
        return WonMessage.of(ret);
    } catch (Exception e) {
        logger.error("Failed to sign", e);
        throw new WonMessageProcessingException("Failed to sign message " + message.getMessageURI().toString());
    }
}
Also used : WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) WonMessage(won.protocol.message.WonMessage) ArrayList(java.util.ArrayList) WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException)

Example 7 with WonMessageProcessingException

use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.

the class SignatureAddingWonMessageProcessor method signWithAtomKey.

/**
 * Used by owners - they find the key alias in the message and get the key from
 * the cryptography service.
 *
 * @param wonMessage
 * @return
 * @throws WonMessageProcessingException
 */
public WonMessage signWithAtomKey(final WonMessage wonMessage) throws WonMessageProcessingException {
    List<WonMessage> ret = new ArrayList<WonMessage>();
    for (WonMessage message : wonMessage.getAllMessages()) {
        // use senderAtom key for signing
        Optional<URI> senderAtomURI = Optional.of(message.getSenderAtomURIRequired());
        String alias = keyPairAliasDerivationStrategy.getAliasForAtomUri(senderAtomURI.get().toString());
        PrivateKey privateKey = cryptographyService.getPrivateKey(alias);
        PublicKey publicKey = cryptographyService.getPublicKey(alias);
        if (privateKey == null || publicKey == null) {
            throw new WonMessageProcessingException(String.format("Cannot sign message with key for atom %s: key not found", senderAtomURI));
        }
        try {
            ret.add(processWithKey(message, senderAtomURI.get().toString(), privateKey, publicKey));
        } catch (Exception e) {
            logger.error("Failed to sign", e);
            throw new WonMessageProcessingException("Failed to sign message " + message.getMessageURI().toString(), e);
        }
    }
    return WonMessage.of(ret);
}
Also used : WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) WonMessage(won.protocol.message.WonMessage) ArrayList(java.util.ArrayList) URI(java.net.URI) WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException)

Example 8 with WonMessageProcessingException

use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.

the class DeactivateAtomMessageFromOwnerReactionProcessor method process.

public void process(final Exchange exchange) throws Exception {
    WonMessage wonMessage = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_HEADER);
    URI recipientAtomURI = wonMessage.getRecipientAtomURI();
    logger.debug("DEACTIVATING atom. atomURI:{}", recipientAtomURI);
    if (recipientAtomURI == null)
        throw new WonMessageProcessingException("recipientAtomURI is not set");
    Atom atom = atomService.getAtomRequired(recipientAtomURI);
    matcherProtocolMatcherClient.atomDeactivated(atom.getAtomURI(), wonMessage);
    // close all connections
    Collection<Connection> conns = connectionRepository.findByAtomURIAndNotStateForUpdate(atom.getAtomURI(), ConnectionState.CLOSED);
    for (Connection con : conns) {
        entityManager.refresh(con);
        closeConnection(atom, con);
    }
}
Also used : WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) URI(java.net.URI) Atom(won.protocol.model.Atom)

Example 9 with WonMessageProcessingException

use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.

the class UriConsistencyCheckingWonMessageProcessor method process.

@Override
public WonMessage process(WonMessage message) {
    StopWatch sw = new StopWatch();
    if (message == null) {
        throw new WonMessageProcessingException("No WonMessage object found in exchange");
    }
    sw.start("validMessageURI");
    if (!WonMessageUtils.isValidMessageUri(message.getMessageURIRequired())) {
        throw new WonMessageNotWellFormedException("Not a valid message URI: " + message.getMessageURI());
    }
    sw.stop();
    sw.start("envelopePropertyCheck");
    EnvelopePropertyCheckResult result = message.checkEnvelopeProperties();
    if (!result.isValid()) {
        throw new WonMessageNotWellFormedException(result.getMessage());
    }
    sw.stop();
    sw.start("getNodeInfo");
    Optional<URI> senderNode = messageRoutingInfoService.senderNode(message);
    Optional<URI> recipientNode = messageRoutingInfoService.recipientNode(message);
    if (!senderNode.isPresent() && !message.getMessageTypeRequired().isHintMessage()) {
        throw new WonMessageProcessingException("Cannot determine sender node for " + message.toShortStringForDebug());
    }
    if (!recipientNode.isPresent()) {
        throw new WonMessageProcessingException("Cannot determine recipient node for " + message.toShortStringForDebug());
    }
    WonNodeInfo senderNodeInfo = null;
    WonNodeInfo recipientNodeInfo = null;
    if (senderNode.isPresent() && !message.getMessageType().isHintMessage()) {
        // do not check the sender node for a hint
        // TODO: change this behaviour as soon as a matcher uses a WoN node
        senderNodeInfo = wonNodeInformationService.getWonNodeInformation(senderNode.get());
    }
    if (recipientNode != null) {
        recipientNodeInfo = wonNodeInformationService.getWonNodeInformation(recipientNode.get());
    }
    if (senderNodeInfo == null && !message.getMessageType().isHintMessage()) {
        throw new WonMessageProcessingException("Could not load sender WonNodeInfo (won node " + senderNode.get() + ")");
    }
    if (recipientNodeInfo == null) {
        throw new WonMessageProcessingException("Could not load recipient WonNodeInfo (won node " + recipientNode.get() + ")");
    }
    sw.stop();
    if (!message.getMessageType().isHintMessage()) {
        sw.start("senderAtomUriCheck");
        checkAtomUri(message.getSenderAtomURI(), senderNodeInfo);
        sw.stop();
        sw.start("senderSocketUriCheck");
        checkSocketUri(message.getSenderSocketURI(), senderNodeInfo);
        sw.stop();
        // there is no way atom or connection uri can be on the recipient node and the
        // recipient node is different from the sender node
        sw.start("atomUriCheck");
        checkAtomUri(message.getAtomURI(), senderNodeInfo);
        sw.stop();
        sw.start("connectionUriCheck");
        checkConnectionUri(message.getConnectionURI(), senderNodeInfo);
        // Check that atom URI for create_atom message corresponds to local pattern
        sw.stop();
        sw.start("createMsgAtomUriCheck");
        checkCreateMsgAtomURI(message, senderNodeInfo);
        sw.stop();
    }
    sw.start("recipientAtomUriCheck");
    checkAtomUri(message.getRecipientAtomURI(), recipientNodeInfo);
    sw.stop();
    sw.start("recipientSocketUriCheck");
    checkSocketUri(message.getRecipientSocketURI(), recipientNodeInfo);
    sw.stop();
    sw.start("signerCheck");
    WonMessageDirection statedDirection = message.getEnvelopeType();
    if (statedDirection.isFromSystem()) {
        if (!Objects.equals(message.getSenderNodeURIRequired(), message.getSignerURIRequired())) {
            RDFDataMgr.write(System.out, message.getCompleteDataset(), Lang.TRIG);
            throw new WonMessageNotWellFormedException("WonMessage " + message.toShortStringForDebug() + " is FROM_SYSTEM but not signed by its node");
        }
    }
    sw.stop();
    if (logger.isDebugEnabled()) {
        logger.debug(LogMarkers.TIMING, "URI Consistency check timing for message {}:\n{}", message.getMessageURIRequired(), sw.prettyPrint());
    }
    return message;
}
Also used : WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException) EnvelopePropertyCheckResult(won.protocol.message.WonMessage.EnvelopePropertyCheckResult) WonNodeInfo(won.protocol.service.WonNodeInfo) WonMessageNotWellFormedException(won.protocol.exception.WonMessageNotWellFormedException) URI(java.net.URI) WonMessageDirection(won.protocol.message.WonMessageDirection) StopWatch(org.springframework.util.StopWatch)

Example 10 with WonMessageProcessingException

use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.

the class SignatureAddingWonMessageProcessor method signWithOtherKey.

public WonMessage signWithOtherKey(final WonMessage wonMessage, URI webId) throws WonMessageProcessingException {
    List<WonMessage> ret = new ArrayList<>();
    for (WonMessage message : wonMessage.getAllMessages()) {
        // use senderAtom key for signing
        String alias = keyPairAliasDerivationStrategy.getAliasForAtomUri(webId.toString());
        PrivateKey privateKey = cryptographyService.getPrivateKey(alias);
        PublicKey publicKey = cryptographyService.getPublicKey(alias);
        Objects.requireNonNull(publicKey);
        Objects.requireNonNull(publicKey);
        try {
            ret.add(processWithKey(message, webId.toString(), privateKey, publicKey));
        } catch (Exception e) {
            logger.error("Failed to sign", e);
            throw new WonMessageProcessingException("Failed to sign message " + message.getMessageURI().toString(), e);
        }
    }
    return WonMessage.of(ret);
}
Also used : WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) WonMessage(won.protocol.message.WonMessage) ArrayList(java.util.ArrayList) WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException)

Aggregations

WonMessageProcessingException (won.protocol.exception.WonMessageProcessingException)18 WonMessage (won.protocol.message.WonMessage)13 URI (java.net.URI)8 PublicKey (java.security.PublicKey)5 Atom (won.protocol.model.Atom)4 Connection (won.protocol.model.Connection)4 PrivateKey (java.security.PrivateKey)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Dataset (org.apache.jena.query.Dataset)3 Annotation (java.lang.annotation.Annotation)2 Test (org.junit.Test)2 StopWatch (org.springframework.util.StopWatch)2 WonMessageType (won.protocol.message.WonMessageType)2 StringWriter (java.io.StringWriter)1 MethodHandles (java.lang.invoke.MethodHandles)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 SignatureException (java.security.SignatureException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1