use of org.olat.core.util.crypto.X509CertificatePrivateKeyPair in project openolat by klemens.
the class XMLDigitalSignatureUtilTest method signAndValidate.
@Test
public void signAndValidate() throws Exception {
X509CertificatePrivateKeyPair certificateInfo = getCertificatePrivateKeyPair();
URL xmlUrl = XMLDigitalSignatureUtilTest.class.getResource("assessmentResult.xml");
File xmlFile = new File(xmlUrl.toURI());
File xmlSignedFile = File.createTempFile("assessment-result", "_signed.xml");
XMLDigitalSignatureUtil.signEmbedded(xmlFile, xmlSignedFile, certificateInfo.getX509Cert(), certificateInfo.getPrivateKey());
Assert.assertTrue(xmlSignedFile.length() > 0);
boolean valid = XMLDigitalSignatureUtil.validate(xmlSignedFile, certificateInfo.getX509Cert().getPublicKey());
Assert.assertTrue(valid);
// clean up
Files.deleteIfExists(xmlSignedFile.toPath());
}
use of org.olat.core.util.crypto.X509CertificatePrivateKeyPair in project OpenOLAT by OpenOLAT.
the class XMLDigitalSignatureUtilTest method signAndValidate.
@Test
public void signAndValidate() throws Exception {
X509CertificatePrivateKeyPair certificateInfo = getCertificatePrivateKeyPair();
URL xmlUrl = XMLDigitalSignatureUtilTest.class.getResource("assessmentResult.xml");
File xmlFile = new File(xmlUrl.toURI());
File xmlSignedFile = File.createTempFile("assessment-result", "_signed.xml");
XMLDigitalSignatureUtil.signEmbedded(xmlFile, xmlSignedFile, certificateInfo.getX509Cert(), certificateInfo.getPrivateKey());
Assert.assertTrue(xmlSignedFile.length() > 0);
boolean valid = XMLDigitalSignatureUtil.validate(xmlSignedFile, certificateInfo.getX509Cert().getPublicKey());
Assert.assertTrue(valid);
// clean up
Files.deleteIfExists(xmlSignedFile.toPath());
}
use of org.olat.core.util.crypto.X509CertificatePrivateKeyPair in project OpenOLAT by OpenOLAT.
the class XMLDigitalSignatureUtilTest method signDetachedAndValidate_exoticUri.
@Test
public void signDetachedAndValidate_exoticUri() throws Exception {
X509CertificatePrivateKeyPair certificateInfo = getCertificatePrivateKeyPair();
URL xmlUrl = XMLDigitalSignatureUtilTest.class.getResource("assessmentResult.xml");
File xmlFile = new File(xmlUrl.toURI());
String xmlUri = "http://localhost:8081/RepositoryEntry/688455680/CourseNode/95133178953589/TestSession/2693/assessmentResult.xml";
File xmlSignatureFile = File.createTempFile("assessment-result", "_signature.xml");
XMLDigitalSignatureUtil.signDetached(xmlUri, xmlFile, xmlSignatureFile, null, null, certificateInfo.getX509Cert(), certificateInfo.getPrivateKey());
Assert.assertTrue(xmlSignatureFile.length() > 0);
boolean valid = XMLDigitalSignatureUtil.validate(xmlUri, xmlFile, xmlSignatureFile, certificateInfo.getX509Cert().getPublicKey());
Assert.assertTrue(valid);
// clean up
Files.deleteIfExists(xmlSignatureFile.toPath());
}
use of org.olat.core.util.crypto.X509CertificatePrivateKeyPair in project OpenOLAT by OpenOLAT.
the class XMLDigitalSignatureUtilTest method signDetachedAndValidate_notValid.
@Test
public void signDetachedAndValidate_notValid() throws Exception {
X509CertificatePrivateKeyPair certificateInfo = getCertificatePrivateKeyPair();
URL xmlUrl = XMLDigitalSignatureUtilTest.class.getResource("assessmentResult.xml");
File xmlFile = new File(xmlUrl.toURI());
String xmlUri = xmlUrl.toURI().toString();
File xmlSignatureFile = File.createTempFile("assessment-result", "_signature.xml");
XMLDigitalSignatureUtil.signDetached(xmlUri, xmlFile, xmlSignatureFile, null, null, certificateInfo.getX509Cert(), certificateInfo.getPrivateKey());
Assert.assertTrue(xmlSignatureFile.length() > 0);
URL xmlTamperedUrl = XMLDigitalSignatureUtilTest.class.getResource("assessmentResult_tampered.xml");
File xmlTamperedFile = new File(xmlTamperedUrl.toURI());
boolean valid = XMLDigitalSignatureUtil.validate(xmlUri, xmlTamperedFile, xmlSignatureFile, certificateInfo.getX509Cert().getPublicKey());
Assert.assertFalse(valid);
// clean up
Files.deleteIfExists(xmlSignatureFile.toPath());
}
use of org.olat.core.util.crypto.X509CertificatePrivateKeyPair in project OpenOLAT by OpenOLAT.
the class XMLDigitalSignatureUtilTest method signDetachedAndValidate_containSignatureDocument.
/**
* Test if the signature can be detached and imported in an other
* DOM structure.
*
* @throws Exception
*/
@Test
public void signDetachedAndValidate_containSignatureDocument() throws Exception {
X509CertificatePrivateKeyPair certificateInfo = getCertificatePrivateKeyPair();
URL xmlUrl = XMLDigitalSignatureUtilTest.class.getResource("assessmentResult.xml");
File xmlFile = new File(xmlUrl.toURI());
String xmlUri = "http://localhost:8081/RepositoryEntry/688455680/CourseNode/95133178953589/TestSession/2693/assessmentResult.xml";
Document signatureDocument = XMLDigitalSignatureUtil.createDocument();
Node rootNode = signatureDocument.appendChild(signatureDocument.createElement("assessmentTestSignature"));
Node courseNode = rootNode.appendChild(signatureDocument.createElement("course"));
courseNode.appendChild(signatureDocument.createTextNode("Very difficult test"));
File xmlSignatureFile = File.createTempFile("assessment-result", "_signature.xml");
XMLDigitalSignatureUtil.signDetached(xmlUri, xmlFile, xmlSignatureFile, signatureDocument, null, certificateInfo.getX509Cert(), certificateInfo.getPrivateKey());
Assert.assertTrue(xmlSignatureFile.length() > 0);
boolean valid = XMLDigitalSignatureUtil.validate(xmlUri, xmlFile, xmlSignatureFile, certificateInfo.getX509Cert().getPublicKey());
Assert.assertTrue(valid);
// load the signature and check that the course info and the Signature is there
Document reloadSignatureDocument = XMLDigitalSignatureUtil.getDocument(xmlSignatureFile);
NodeList courseNl = reloadSignatureDocument.getElementsByTagName("course");
Assert.assertEquals(1, courseNl.getLength());
NodeList signatureNl = reloadSignatureDocument.getElementsByTagName("Signature");
Assert.assertEquals(1, signatureNl.getLength());
// clean up
Files.deleteIfExists(xmlSignatureFile.toPath());
}
Aggregations