Search in sources :

Example 16 with DIDAuthenticate

use of iso.std.iso_iec._24727.tech.schema.DIDAuthenticate in project open-ecard by ecsec.

the class GenericCryptographyProtocolTest method testVerifySignature.

/**
 * Test for the VerifySignature Step of the Generic Cryptography protocol. After we connected to the ESIGN
 * application of the eGK, we use DIDList to get a List of DIDs that support the compute signature function. We
 * then authenticate with PIN.home and let the card sign our message. Afterwards we call VerifySignature for that
 * signature which should return OK.
 *
 * @throws Exception
 *             when something in this test went unexpectedly wrong
 */
@Test(enabled = TESTS_ENABLED)
public void testVerifySignature() throws Exception {
    CardApplicationPath cardApplicationPath = new CardApplicationPath();
    CardApplicationPathType cardApplicationPathType = new CardApplicationPathType();
    cardApplicationPathType.setCardApplication(cardApplication);
    cardApplicationPath.setCardAppPathRequest(cardApplicationPathType);
    CardApplicationPathResponse cardApplicationPathResponse = instance.cardApplicationPath(cardApplicationPath);
    WSHelper.checkResult(cardApplicationPathResponse);
    CardApplicationConnect parameters = new CardApplicationConnect();
    CardAppPathResultSet cardAppPathResultSet = cardApplicationPathResponse.getCardAppPathResultSet();
    parameters.setCardApplicationPath(cardAppPathResultSet.getCardApplicationPathResult().get(0));
    CardApplicationConnectResponse result = instance.cardApplicationConnect(parameters);
    WSHelper.checkResult(result);
    assertEquals(ECardConstants.Major.OK, result.getResult().getResultMajor());
    DIDList didList = new DIDList();
    didList.setConnectionHandle(result.getConnectionHandle());
    DIDQualifierType didQualifier = new DIDQualifierType();
    didQualifier.setApplicationIdentifier(cardApplication);
    didQualifier.setObjectIdentifier(ECardConstants.Protocol.GENERIC_CRYPTO);
    didQualifier.setApplicationFunction("Compute-signature");
    didList.setFilter(didQualifier);
    DIDListResponse didListResponse = instance.didList(didList);
    assertTrue(didListResponse.getDIDNameList().getDIDName().size() > 0);
    WSHelper.checkResult(didListResponse);
    DIDAuthenticate didAthenticate = new DIDAuthenticate();
    didAthenticate.setDIDName("PIN.home");
    PinCompareDIDAuthenticateInputType didAuthenticationData = new PinCompareDIDAuthenticateInputType();
    didAthenticate.setAuthenticationProtocolData(didAuthenticationData);
    didAthenticate.setConnectionHandle(result.getConnectionHandle());
    didAthenticate.getConnectionHandle().setCardApplication(cardApplication_ROOT);
    didAuthenticationData.setProtocol(ECardConstants.Protocol.PIN_COMPARE);
    didAthenticate.setAuthenticationProtocolData(didAuthenticationData);
    DIDAuthenticateResponse didAuthenticateResult = instance.didAuthenticate(didAthenticate);
    WSHelper.checkResult(didAuthenticateResult);
    assertEquals(didAuthenticateResult.getAuthenticationProtocolData().getProtocol(), ECardConstants.Protocol.PIN_COMPARE);
    assertEquals(didAuthenticateResult.getAuthenticationProtocolData().getAny().size(), 0);
    assertEquals(ECardConstants.Major.OK, didAuthenticateResult.getResult().getResultMajor());
    for (int numOfDIDs = 0; numOfDIDs < didListResponse.getDIDNameList().getDIDName().size(); numOfDIDs++) {
        String didName = didListResponse.getDIDNameList().getDIDName().get(numOfDIDs);
        DIDGet didGet = new DIDGet();
        didGet.setDIDName(didName);
        didGet.setDIDScope(DIDScopeType.LOCAL);
        didGet.setConnectionHandle(result.getConnectionHandle());
        didGet.getConnectionHandle().setCardApplication(cardApplication);
        DIDGetResponse didGetResponse = instance.didGet(didGet);
        Sign sign = new Sign();
        byte[] message = new byte[] { 0x01, 0x02, 0x03 };
        org.openecard.crypto.common.sal.did.CryptoMarkerType cryptoMarker = new org.openecard.crypto.common.sal.did.CryptoMarkerType((CryptoMarkerType) didGetResponse.getDIDStructure().getDIDMarker());
        String algorithmIdentifier = cryptoMarker.getAlgorithmInfo().getAlgorithmIdentifier().getAlgorithm();
        if (algorithmIdentifier.equals(GenericCryptoUris.RSASSA_PSS_SHA256)) {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            message = messageDigest.digest(message);
        } else if (algorithmIdentifier.equals(GenericCryptoUris.RSA_ENCRYPTION)) {
        // do nothing
        } else {
            LOG.warn("Skipping decipher for the unsupported algorithmIdentifier: {}", algorithmIdentifier);
            continue;
        }
        sign.setMessage(message);
        sign.setConnectionHandle(result.getConnectionHandle());
        sign.getConnectionHandle().setCardApplication(cardApplication);
        sign.setDIDName(didName);
        sign.setDIDScope(DIDScopeType.LOCAL);
        SignResponse signResponse = instance.sign(sign);
        assertEquals(ECardConstants.Major.OK, signResponse.getResult().getResultMajor());
        WSHelper.checkResult(signResponse);
        byte[] signature = signResponse.getSignature();
        VerifySignature verifySignature = new VerifySignature();
        verifySignature.setConnectionHandle(sign.getConnectionHandle());
        verifySignature.setDIDName(didName);
        verifySignature.setDIDScope(DIDScopeType.LOCAL);
        verifySignature.setMessage(message);
        verifySignature.setSignature(signature);
        VerifySignatureResponse verifySignatureResponse = instance.verifySignature(verifySignature);
        WSHelper.checkResult(verifySignatureResponse);
    }
}
Also used : DIDList(iso.std.iso_iec._24727.tech.schema.DIDList) PinCompareDIDAuthenticateInputType(iso.std.iso_iec._24727.tech.schema.PinCompareDIDAuthenticateInputType) CardAppPathResultSet(iso.std.iso_iec._24727.tech.schema.CardApplicationPathResponse.CardAppPathResultSet) DIDListResponse(iso.std.iso_iec._24727.tech.schema.DIDListResponse) CardApplicationPathType(iso.std.iso_iec._24727.tech.schema.CardApplicationPathType) CardApplicationConnect(iso.std.iso_iec._24727.tech.schema.CardApplicationConnect) SignResponse(iso.std.iso_iec._24727.tech.schema.SignResponse) DIDGet(iso.std.iso_iec._24727.tech.schema.DIDGet) MessageDigest(java.security.MessageDigest) DIDAuthenticate(iso.std.iso_iec._24727.tech.schema.DIDAuthenticate) CardApplicationPathResponse(iso.std.iso_iec._24727.tech.schema.CardApplicationPathResponse) DIDQualifierType(iso.std.iso_iec._24727.tech.schema.DIDQualifierType) DIDGetResponse(iso.std.iso_iec._24727.tech.schema.DIDGetResponse) CryptoMarkerType(iso.std.iso_iec._24727.tech.schema.CryptoMarkerType) CardApplicationConnectResponse(iso.std.iso_iec._24727.tech.schema.CardApplicationConnectResponse) VerifySignatureResponse(iso.std.iso_iec._24727.tech.schema.VerifySignatureResponse) VerifySignature(iso.std.iso_iec._24727.tech.schema.VerifySignature) CardApplicationPath(iso.std.iso_iec._24727.tech.schema.CardApplicationPath) DIDAuthenticateResponse(iso.std.iso_iec._24727.tech.schema.DIDAuthenticateResponse) Sign(iso.std.iso_iec._24727.tech.schema.Sign) Test(org.testng.annotations.Test)

Example 17 with DIDAuthenticate

use of iso.std.iso_iec._24727.tech.schema.DIDAuthenticate in project open-ecard by ecsec.

the class SchemaValidationTest method testDIDAuth.

@Test
public void testDIDAuth() throws JAXBException, SAXException, IOException, ObjectValidatorException, ParserConfigurationException {
    JAXBSchemaValidator validator;
    JAXBContext jc = JAXBContext.newInstance(DIDAuthenticate.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    InputStream dataStream = FileUtils.resolveResourceAsStream(SchemaValidationTest.class, "DIDAuthenticate.xml");
    DIDAuthenticate didAuth = (DIDAuthenticate) unmarshaller.unmarshal(dataStream);
    validator = JAXBSchemaValidator.load(didAuth.getClass(), "ISO24727-Protocols.xsd");
    Assert.assertEquals(validator.validateObject(didAuth), true);
    dataStream = FileUtils.resolveResourceAsStream(SchemaValidationTest.class, "DIDAuthenticate.xml");
    DIDAuthenticate didAuth2 = (DIDAuthenticate) unmarshaller.unmarshal(dataStream);
    DIDAuthenticationDataType authData = didAuth2.getAuthenticationProtocolData();
    DocumentBuilderFactory docFac = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFac.newDocumentBuilder();
    Document d = docBuilder.newDocument();
    Element sigElem = d.createElementNS("urn:iso:std:iso-iec:24727:tech:schema", "Signature");
    sigElem.setTextContent("1254786930AAD4A8");
    authData.getAny().add(sigElem);
    didAuth2.setAuthenticationProtocolData(authData);
    validator = JAXBSchemaValidator.load(didAuth2.getClass(), "ISO24727-Protocols.xsd");
    Assert.assertEquals(validator.validateObject(didAuth2), false);
}
Also used : DIDAuthenticate(iso.std.iso_iec._24727.tech.schema.DIDAuthenticate) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) DIDAuthenticationDataType(iso.std.iso_iec._24727.tech.schema.DIDAuthenticationDataType) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Document(org.w3c.dom.Document) Test(org.testng.annotations.Test)

Aggregations

DIDAuthenticateResponse (iso.std.iso_iec._24727.tech.schema.DIDAuthenticateResponse)15 DIDAuthenticate (iso.std.iso_iec._24727.tech.schema.DIDAuthenticate)12 Test (org.testng.annotations.Test)8 CardApplicationConnect (iso.std.iso_iec._24727.tech.schema.CardApplicationConnect)6 CardApplicationConnectResponse (iso.std.iso_iec._24727.tech.schema.CardApplicationConnectResponse)6 CardApplicationPath (iso.std.iso_iec._24727.tech.schema.CardApplicationPath)6 CardApplicationPathResponse (iso.std.iso_iec._24727.tech.schema.CardApplicationPathResponse)6 CardApplicationPathType (iso.std.iso_iec._24727.tech.schema.CardApplicationPathType)6 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)6 DIDAuthenticationDataType (iso.std.iso_iec._24727.tech.schema.DIDAuthenticationDataType)6 Transmit (iso.std.iso_iec._24727.tech.schema.Transmit)4 TransmitResponse (iso.std.iso_iec._24727.tech.schema.TransmitResponse)4 Result (oasis.names.tc.dss._1_0.core.schema.Result)4 PINCompareDIDAuthenticateInputType (org.openecard.common.anytype.pin.PINCompareDIDAuthenticateInputType)4 BigInteger (java.math.BigInteger)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 DynamicContext (org.openecard.common.DynamicContext)3 ObjectSchemaValidator (org.openecard.common.interfaces.ObjectSchemaValidator)3 ObjectValidatorException (org.openecard.common.interfaces.ObjectValidatorException)3 Promise (org.openecard.common.util.Promise)3