Search in sources :

Example 1 with SignatureVerificationState

use of won.cryptography.rdfsign.SignatureVerificationState in project webofneeds by researchstudio-sat.

the class WonMessageSignerVerifierTest method signAndVerifySignedMessageNode.

@Test
public void signAndVerifySignedMessageNode() throws Exception {
    // create signed dataset
    Dataset testDataset = TestSigningUtils.prepareTestDataset(RESOURCE_NODE_FILE_NOSIG);
    WonMessage testMsg = new WonMessage(testDataset);
    // sign
    testMsg = WonMessageSignerVerifier.sign(nodeKey, pubKeysMap.get(TestSigningUtils.nodeCertUri), TestSigningUtils.nodeCertUri, testMsg);
    // pretend msg was serialized and deserialized in between
    // pretend it was serialized and deserialized
    String datasetString = RdfUtils.writeDatasetToString(testMsg.getCompleteDataset(), Lang.TRIG);
    testMsg = new WonMessage(RdfUtils.readDatasetFromString(datasetString, Lang.TRIG));
    // verify
    SignatureVerificationState result = WonMessageSignerVerifier.verify(pubKeysMap, testMsg);
    Assert.assertTrue(result.getMessage(), result.isVerificationPassed());
    Assert.assertEquals(3, result.getSignatureGraphNames().size());
    Assert.assertEquals(NEED_CORE_DATA_URI, result.getSignedGraphName(NEED_CORE_DATA_SIG_URI));
    Assert.assertEquals(EVENT_ENV1_URI, result.getSignedGraphName(EVENT_ENV1_SIG_URI));
    Assert.assertEquals(EVENT_ENV2_URI, result.getSignedGraphName(EVENT_ENV2_SIG_URI));
    // write for debugging
    TestSigningUtils.writeToTempFile(testMsg.getCompleteDataset());
}
Also used : Dataset(org.apache.jena.query.Dataset) WonMessage(won.protocol.message.WonMessage) SignatureVerificationState(won.cryptography.rdfsign.SignatureVerificationState) Test(org.junit.Test)

Example 2 with SignatureVerificationState

use of won.cryptography.rdfsign.SignatureVerificationState in project webofneeds by researchstudio-sat.

the class SignatureCheckingWonMessageProcessor method process.

@Override
public WonMessage process(final WonMessage message) throws WonMessageProcessingException {
    SignatureVerificationState result = null;
    try {
        // obtain public keys
        Map<String, PublicKey> keys = getRequiredPublicKeys(message.getCompleteDataset());
        // verify with those public keys
        result = WonMessageSignerVerifier.verify(keys, message);
        logger.debug("VERIFIED=" + result.isVerificationPassed() + " with keys: " + keys.values() + " for\n" + RdfUtils.writeDatasetToString(message.getCompleteDataset(), Lang.TRIG));
    } catch (Exception e) {
        // TODO SignatureProcessingException?
        throw new WonMessageProcessingException("Could not verify message " + message.getMessageURI(), e);
    }
    // throw exception if the verification fails:
    if (!result.isVerificationPassed()) {
        // TODO SignatureProcessingException?
        throw new WonMessageProcessingException(new SignatureException("Could not verify message " + message.getMessageURI() + ": " + result.getMessage()));
    }
    return message;
}
Also used : WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) PublicKey(java.security.PublicKey) SignatureVerificationState(won.cryptography.rdfsign.SignatureVerificationState) SignatureException(java.security.SignatureException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SignatureException(java.security.SignatureException) WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 3 with SignatureVerificationState

use of won.cryptography.rdfsign.SignatureVerificationState in project webofneeds by researchstudio-sat.

the class WonMessageSignerVerifierTest method signAndVerifyUnsignedMessage.

@Test
@Ignore
public void signAndVerifyUnsignedMessage() throws Exception {
    // create signed dataset
    Dataset testDataset = TestSigningUtils.prepareTestDataset(RESOURCE_OWNER_FILE_NOSIG);
    WonMessage testMsg = new WonMessage(testDataset);
    // sign
    testMsg = WonMessageSignerVerifier.sign(needKey, pubKeysMap.get(TestSigningUtils.nodeCertUri), TestSigningUtils.needCertUri, testMsg);
    // pretend msg was serialized and deserialized in between
    // pretend it was serialized and deserialized
    String datasetString = RdfUtils.writeDatasetToString(testMsg.getCompleteDataset(), Lang.TRIG);
    testMsg = new WonMessage(RdfUtils.readDatasetFromString(datasetString, Lang.TRIG));
    // verify
    SignatureVerificationState result = WonMessageSignerVerifier.verify(pubKeysMap, testMsg);
    Assert.assertTrue(result.isVerificationPassed());
    Assert.assertEquals(2, result.getSignatureGraphNames().size());
    Assert.assertEquals(NEED_CORE_DATA_URI, result.getSignedGraphName(NEED_CORE_DATA_SIG_URI));
    Assert.assertEquals(EVENT_ENV1_URI, result.getSignedGraphName(EVENT_ENV1_SIG_URI));
    // write for debugging
    TestSigningUtils.writeToTempFile(testMsg.getCompleteDataset());
}
Also used : Dataset(org.apache.jena.query.Dataset) WonMessage(won.protocol.message.WonMessage) SignatureVerificationState(won.cryptography.rdfsign.SignatureVerificationState) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with SignatureVerificationState

use of won.cryptography.rdfsign.SignatureVerificationState in project webofneeds by researchstudio-sat.

the class WonMessageSignerVerifierTest method testVerify.

@Test
public void testVerify() throws Exception {
    // create signed dataset
    Dataset testDataset = TestSigningUtils.prepareTestDataset(RESOURCE_FILE_SIG);
    WonMessage testMsg = new WonMessage(testDataset);
    // verify
    SignatureVerificationState result = WonMessageSignerVerifier.verify(pubKeysMap, testMsg);
    Assert.assertTrue(result.getMessage(), result.isVerificationPassed());
    Assert.assertEquals(3, result.getSignatureGraphNames().size());
    Assert.assertEquals(NEED_CORE_DATA_URI, result.getSignedGraphName(NEED_CORE_DATA_SIG_URI));
    Assert.assertEquals(EVENT_ENV1_URI, result.getSignedGraphName(EVENT_ENV1_SIG_URI));
    Assert.assertEquals(EVENT_ENV2_URI, result.getSignedGraphName(EVENT_ENV2_SIG_URI));
}
Also used : Dataset(org.apache.jena.query.Dataset) WonMessage(won.protocol.message.WonMessage) SignatureVerificationState(won.cryptography.rdfsign.SignatureVerificationState) Test(org.junit.Test)

Aggregations

SignatureVerificationState (won.cryptography.rdfsign.SignatureVerificationState)4 Dataset (org.apache.jena.query.Dataset)3 Test (org.junit.Test)3 WonMessage (won.protocol.message.WonMessage)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 PublicKey (java.security.PublicKey)1 SignatureException (java.security.SignatureException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 Ignore (org.junit.Ignore)1 WonMessageProcessingException (won.protocol.message.processor.exception.WonMessageProcessingException)1