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));
}
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;
}
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;
}
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;
}
Aggregations