Search in sources :

Example 6 with X509CertificatePrivateKeyPair

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());
}
Also used : X509CertificatePrivateKeyPair(org.olat.core.util.crypto.X509CertificatePrivateKeyPair) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 7 with X509CertificatePrivateKeyPair

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());
}
Also used : X509CertificatePrivateKeyPair(org.olat.core.util.crypto.X509CertificatePrivateKeyPair) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 8 with X509CertificatePrivateKeyPair

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());
}
Also used : X509CertificatePrivateKeyPair(org.olat.core.util.crypto.X509CertificatePrivateKeyPair) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 9 with X509CertificatePrivateKeyPair

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());
}
Also used : X509CertificatePrivateKeyPair(org.olat.core.util.crypto.X509CertificatePrivateKeyPair) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 10 with X509CertificatePrivateKeyPair

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());
}
Also used : X509CertificatePrivateKeyPair(org.olat.core.util.crypto.X509CertificatePrivateKeyPair) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Aggregations

X509CertificatePrivateKeyPair (org.olat.core.util.crypto.X509CertificatePrivateKeyPair)20 File (java.io.File)16 Test (org.junit.Test)14 URL (java.net.URL)12 Document (org.w3c.dom.Document)6 IOException (java.io.IOException)4 TransformerException (javax.xml.transform.TransformerException)4 OLATRuntimeException (org.olat.core.logging.OLATRuntimeException)4 QtiXmlInterpretationException (uk.ac.ed.ph.jqtiplus.reading.QtiXmlInterpretationException)4 XmlResourceNotFoundException (uk.ac.ed.ph.jqtiplus.xmlutils.XmlResourceNotFoundException)4 ArrayList (java.util.ArrayList)2 MailBundle (org.olat.core.util.mail.MailBundle)2 AssessmentTestSession (org.olat.ims.qti21.AssessmentTestSession)2 DigitalSignatureValidation (org.olat.ims.qti21.model.DigitalSignatureValidation)2 InMemoryAssessmentTestSession (org.olat.ims.qti21.model.InMemoryAssessmentTestSession)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2