Search in sources :

Example 1 with SignatureData

use of de.uni_koblenz.aggrimm.icp.crypto.sign.graph.SignatureData in project webofneeds by researchstudio-sat.

the class WonAssemblerTest method testAssembleOneGraphSignature.

@Test
public void testAssembleOneGraphSignature() throws Exception {
    // The Signingframework reader cannot reproduce the correct graph
    // structure, it has problems with blank nodes [] parts.
    // GraphCollection gc = TriGPlusReader.readFile(inFile);
    // create dataset that contains need core data graph
    Dataset testDataset = TestSigningUtils.prepareTestDatasetFromNamedGraphs(RESOURCE_FILE, new String[] { NEED_CORE_DATA_URI });
    // convert to graph collection
    GraphCollection gc = ModelConverter.modelToGraphCollection(NEED_CORE_DATA_URI, testDataset);
    // create mock signature
    SignatureData mockSigData = createMockSignature();
    gc.setSignature(mockSigData);
    // test assemble()
    WonAssembler.assemble(gc, testDataset, NEED_CORE_DATA_SIG_URI);
    // use for debugging output
    // TestSigningUtils.writeToTempFile(testDataset);
    // extract names of the named graphs
    List<String> namesList = RdfUtils.getModelNames(testDataset);
    // do some checks to make sure there is 1 signed names graph
    Assert.assertEquals("should be one named graph with data and one named graph with signature", 2, namesList.size());
    Assert.assertTrue("should be some triples in signature graph", testDataset.getNamedModel(NEED_CORE_DATA_SIG_URI).listStatements().hasNext());
    Assert.assertTrue("should be no triples in default graph", !testDataset.getDefaultModel().listStatements().hasNext());
    int triplesCounter = TestSigningUtils.countTriples(testDataset.getNamedModel(NEED_CORE_DATA_SIG_URI).listStatements());
    Set<String> subjs = TestSigningUtils.getSubjects(testDataset.getNamedModel(NEED_CORE_DATA_SIG_URI));
    Set<String> objs = TestSigningUtils.getUriResourceObjects(testDataset.getNamedModel(NEED_CORE_DATA_SIG_URI));
    Assert.assertEquals("signature graph should contain 11 triples", 11, triplesCounter);
    Assert.assertTrue("signed graph name should be an object in signature triples", objs.contains(NEED_CORE_DATA_URI));
    Assert.assertTrue("signature graph name should be a subject in signature triples", subjs.contains(NEED_CORE_DATA_SIG_URI));
}
Also used : GraphCollection(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection) SignatureData(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.SignatureData) Dataset(org.apache.jena.query.Dataset) Test(org.junit.Test)

Example 2 with SignatureData

use of de.uni_koblenz.aggrimm.icp.crypto.sign.graph.SignatureData in project webofneeds by researchstudio-sat.

the class WonAssemblerTest method createMockSignature.

private SignatureData createMockSignature() throws NoSuchAlgorithmException {
    SignatureData mockSigData = new SignatureData();
    mockSigData.setHash(new BigInteger(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }));
    mockSigData.setSignature("\"blahblahSignature\"");
    mockSigData.setDigestGen(MessageDigest.getInstance("sha-256"));
    mockSigData.setCanonicalizationMethod("blahblahCanonicalizationMethod");
    mockSigData.setGraphDigestMethod("blahblahGraphDigestMethod");
    mockSigData.setSerializationMethod("blahblahSerializationmethod");
    mockSigData.setSignatureMethod("blahblahSigMathod");
    // mockSigData.setVerificationCertificateUri("\"blahblahVerificationCertificate\"");
    mockSigData.setVerificationCertificate("<http://localhost:8080/blahblah/certificate>");
    return mockSigData;
}
Also used : SignatureData(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.SignatureData) BigInteger(java.math.BigInteger)

Example 3 with SignatureData

use of de.uni_koblenz.aggrimm.icp.crypto.sign.graph.SignatureData in project webofneeds by researchstudio-sat.

the class WonSigner method sign.

/**
 * Signs the graphs of the dataset with the provided private key and referencing
 * the provided certificate/public key uri in signature, this uri will be used
 * to extract key by the verification party.
 *
 * @param privateKey the private key
 * @param cert the certificate reference (where the public key can be found for verification)
 * @param graphsToSign the names of the graphs that have to be signed. If not provided -
 * all the graphs that don't have signatures will be signed.
 * @throws Exception
 */
// TODO chng exceptions to won exceptions?
public List<WonSignatureData> sign(PrivateKey privateKey, String cert, PublicKey publicKey, String... graphsToSign) throws Exception {
    List<WonSignatureData> sigRefs = new ArrayList<>(graphsToSign.length);
    MessageDigest md = MessageDigest.getInstance(ENV_HASH_ALGORITHM, SIGNING_ALGORITHM_PROVIDER);
    String fingerprint = Base64.getEncoder().encodeToString(md.digest(publicKey.getEncoded()));
    for (String signedGraphUri : graphsToSign) {
        // TODO should be generated in a more proper way and not here - check of the name already exists etc.
        if (logger.isDebugEnabled()) {
            StringWriter sw = new StringWriter();
            RDFDataMgr.write(sw, dataset.getNamedModel(signedGraphUri), Lang.TRIG);
            logger.debug("signing graph {} with content: {}", graphsToSign, sw.toString());
        }
        String signatureUri = signedGraphUri + "-sig";
        // create GraphCollection with one NamedGraph that corresponds to this Model
        GraphCollection inputGraph = ModelConverter.modelToGraphCollection(signedGraphUri, dataset);
        // sign the NamedGraph inside that GraphCollection
        SignatureData sigValue = signNamedGraph(inputGraph, privateKey, cert);
        String hash = new String(Base64.getEncoder().encodeToString(sigValue.getHash().toByteArray()));
        WonSignatureData sigRef = new WonSignatureData(signedGraphUri, signatureUri, sigValue.getSignature(), hash, fingerprint, cert);
        sigRefs.add(sigRef);
    }
    return sigRefs;
}
Also used : GraphCollection(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection) WonSignatureData(won.protocol.message.WonSignatureData) SignatureData(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.SignatureData) StringWriter(java.io.StringWriter) WonSignatureData(won.protocol.message.WonSignatureData) ArrayList(java.util.ArrayList)

Example 4 with SignatureData

use of de.uni_koblenz.aggrimm.icp.crypto.sign.graph.SignatureData in project webofneeds by researchstudio-sat.

the class WonSigner method sign.

private SignatureData sign(GraphCollection gc, PrivateKey privateKey, String verificationCertificate) throws Exception {
    if (verificationCertificate == null) {
        verificationCertificate = "\"cert\"";
    } else {
        verificationCertificate = "<" + verificationCertificate + ">";
    }
    // Signature Data existing?
    if (!gc.hasSignature()) {
        throw new Exception("GraphCollection has no signature data. Call 'canonicalize' and 'hash' methods first.");
    }
    // Get Signature Data
    SignatureData sigData = gc.getSignature();
    // Sign
    Signature sig = Signature.getInstance(SIGNING_ALGORITHM_NAME, SIGNING_ALGORITHM_PROVIDER);
    sig.initSign(privateKey);
    sig.update(sigData.getHash().toByteArray());
    byte[] signatureBytes = sig.sign();
    // String signature = new BASE64Encoder().encode(signatureBytes);
    String signature = Base64.getEncoder().encodeToString(signatureBytes);
    // Update Signature Data
    sigData.setSignature(signature);
    sigData.setSignatureMethod(privateKey.getAlgorithm().toLowerCase());
    sigData.setVerificationCertificate(verificationCertificate);
    return sigData;
}
Also used : WonSignatureData(won.protocol.message.WonSignatureData) SignatureData(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.SignatureData)

Aggregations

SignatureData (de.uni_koblenz.aggrimm.icp.crypto.sign.graph.SignatureData)4 GraphCollection (de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection)2 WonSignatureData (won.protocol.message.WonSignatureData)2 StringWriter (java.io.StringWriter)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Dataset (org.apache.jena.query.Dataset)1 Test (org.junit.Test)1