Search in sources :

Example 31 with Result

use of oasis.names.tc.dss._1_0.core.schema.Result in project open-ecard by ecsec.

the class GenericCryptographyProtocolTest method testDecipher.

/**
 * Test for the Decipher 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 Decipher function. We then authenticate with
 * PIN.home and read the contents of the DIDs certificate. With it's public key we encrypt the contents of
 * plaintext.txt and finally let the card decrypt it through a call to Decipher. In the end we match the result with
 * the original plaintext.
 *
 * @throws Exception when something in this test went unexpectedly wrong
 */
@Test(enabled = TESTS_ENABLED)
public void testDecipher() 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("Decipher");
    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());
    byte[] plaintextBytes = plaintext.getBytes();
    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);
        org.openecard.crypto.common.sal.did.CryptoMarkerType cryptoMarker = new org.openecard.crypto.common.sal.did.CryptoMarkerType((CryptoMarkerType) didGetResponse.getDIDStructure().getDIDMarker());
        ByteArrayOutputStream ciphertext = new ByteArrayOutputStream();
        // read the certificate
        DSIRead dsiRead = new DSIRead();
        dsiRead.setConnectionHandle(result.getConnectionHandle());
        dsiRead.getConnectionHandle().setCardApplication(cardApplication);
        dsiRead.setDSIName(cryptoMarker.getCertificateRefs().get(0).getDataSetName());
        DSIReadResponse dsiReadResponse = instance.dsiRead(dsiRead);
        assertEquals(ECardConstants.Major.OK, dsiReadResponse.getResult().getResultMajor());
        assertTrue(dsiReadResponse.getDSIContent().length > 0);
        // convert the contents to a certificate
        Certificate cert = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(dsiReadResponse.getDSIContent()));
        Cipher cipher;
        int blocksize;
        String algorithmUri = cryptoMarker.getAlgorithmInfo().getAlgorithmIdentifier().getAlgorithm();
        if (algorithmUri.equals(GenericCryptoUris.RSA_ENCRYPTION)) {
            cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.ENCRYPT_MODE, cert);
            // keysize/8-pkcspadding = (2048)/8-11
            blocksize = 245;
        } else if (algorithmUri.equals(GenericCryptoUris.RSAES_OAEP)) {
            cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding", new BouncyCastleProvider());
            cipher.init(Cipher.ENCRYPT_MODE, cert);
            blocksize = cipher.getBlockSize();
        } else {
            LOG.warn("Skipping decipher for the unsupported algorithmOID: {}", algorithmUri);
            continue;
        }
        int rest = plaintextBytes.length % blocksize;
        // encrypt block for block
        for (int offset = 0; offset < plaintextBytes.length; offset += blocksize) {
            if ((offset + blocksize) > plaintextBytes.length) {
                ciphertext.write(cipher.doFinal(plaintextBytes, offset, rest));
            } else {
                ciphertext.write(cipher.doFinal(plaintextBytes, offset, blocksize));
            }
        }
        Decipher decipher = new Decipher();
        decipher.setCipherText(ciphertext.toByteArray());
        decipher.setConnectionHandle(result.getConnectionHandle());
        decipher.getConnectionHandle().setCardApplication(cardApplication);
        decipher.setDIDName(didName);
        decipher.setDIDScope(DIDScopeType.LOCAL);
        DecipherResponse decipherResponse = instance.decipher(decipher);
        assertEquals(decipherResponse.getPlainText(), plaintextBytes);
        // test invalid ciphertext length (not divisible through blocksize without rest)
        decipher = new Decipher();
        decipher.setCipherText(ByteUtils.concatenate((byte) 0x00, ciphertext.toByteArray()));
        decipher.setConnectionHandle(result.getConnectionHandle());
        decipher.getConnectionHandle().setCardApplication(cardApplication);
        decipher.setDIDName(didName);
        decipher.setDIDScope(DIDScopeType.LOCAL);
        decipherResponse = instance.decipher(decipher);
        Result res = decipherResponse.getResult();
        assertEquals(res.getResultMajor(), ECardConstants.Major.ERROR);
        assertEquals(res.getResultMinor(), ECardConstants.Minor.App.INCORRECT_PARM);
    }
}
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) Result(oasis.names.tc.dss._1_0.core.schema.Result) CardApplicationPathType(iso.std.iso_iec._24727.tech.schema.CardApplicationPathType) CardApplicationConnect(iso.std.iso_iec._24727.tech.schema.CardApplicationConnect) DIDGet(iso.std.iso_iec._24727.tech.schema.DIDGet) BouncyCastleProvider(org.openecard.bouncycastle.jce.provider.BouncyCastleProvider) DIDAuthenticate(iso.std.iso_iec._24727.tech.schema.DIDAuthenticate) CardApplicationPathResponse(iso.std.iso_iec._24727.tech.schema.CardApplicationPathResponse) DSIRead(iso.std.iso_iec._24727.tech.schema.DSIRead) 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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) X509Certificate(java.security.cert.X509Certificate) CardApplicationPath(iso.std.iso_iec._24727.tech.schema.CardApplicationPath) DIDAuthenticateResponse(iso.std.iso_iec._24727.tech.schema.DIDAuthenticateResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) DecipherResponse(iso.std.iso_iec._24727.tech.schema.DecipherResponse) Cipher(javax.crypto.Cipher) Decipher(iso.std.iso_iec._24727.tech.schema.Decipher) DSIReadResponse(iso.std.iso_iec._24727.tech.schema.DSIReadResponse) X509Certificate(java.security.cert.X509Certificate) VerifyCertificate(iso.std.iso_iec._24727.tech.schema.VerifyCertificate) Certificate(java.security.cert.Certificate) Test(org.testng.annotations.Test)

Example 32 with Result

use of oasis.names.tc.dss._1_0.core.schema.Result in project open-ecard by ecsec.

the class SALProtocolBaseImpl method perform.

private static <Req extends RequestType> ResponseType perform(Class<? extends ResponseType> responseClass, ProtocolStep step, Req request, TreeMap<String, Object> internalData) {
    // return not implemented result first
    if (step == null) {
        String msg = "There is no applicable protocol step at this point in the protocol flow.";
        Result r = WSHelper.makeResultError(ECardConstants.Minor.SAL.INAPPROPRIATE_PROTOCOL_FOR_ACTION, msg);
        return WSHelper.makeResponse(responseClass, r);
    } else {
        return step.perform(request, internalData);
    }
}
Also used : Result(oasis.names.tc.dss._1_0.core.schema.Result)

Example 33 with Result

use of oasis.names.tc.dss._1_0.core.schema.Result in project tesb-rt-se by Talend.

the class LibraryServerImpl method seekBook.

@Override
public ListOfBooks seekBook(SearchFor body) throws SeekBookError {
    System.out.println("***************************************************************");
    System.out.println("*** seekBook request (Request-Response operation) is received *");
    System.out.println("***************************************************************");
    showSeekBookRequest(body);
    List<String> authorsLastNames = body.getAuthorLastName();
    if (authorsLastNames != null && authorsLastNames.size() > 0) {
        String authorsLastName = authorsLastNames.get(0);
        if (authorsLastName != null && authorsLastName.length() > 0 && !"Icebear".equalsIgnoreCase(authorsLastName)) {
            SeekBookError e = prepareException("No book available from author " + authorsLastName);
            System.out.println("No book available from author " + authorsLastName);
            System.out.println("\nSending business fault (SeekBook error) with parameters:");
            throw e;
        }
    }
    ListOfBooks result = new ListOfBooks();
    BookType book = new BookType();
    result.getBook().add(book);
    PersonType author = new PersonType();
    book.getAuthor().add(author);
    author.setFirstName("Jack");
    author.setLastName("Icebear");
    Calendar dateOfBirth = new GregorianCalendar(101, Calendar.JANUARY, 2);
    author.setDateOfBirth(dateOfBirth.getTime());
    book.getTitle().add("Survival in the Arctic");
    book.getPublisher().add("Frosty Edition");
    book.setYearPublished("2010");
    System.out.println("Book(s) is found:");
    showSeekBookResponse(result);
    return result;
}
Also used : BookType(org.talend.types.test.library.common._1.BookType) SeekBookError(org.talend.services.test.library._1_0.SeekBookError) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) PersonType(org.talend.types.test.library.common._1.PersonType) ListOfBooks(org.talend.types.test.library.common._1.ListOfBooks)

Example 34 with Result

use of oasis.names.tc.dss._1_0.core.schema.Result in project tesb-rt-se by Talend.

the class LibraryServerImpl method seekBook.

@Override
public ListOfBooks seekBook(SearchFor body) throws SeekBookError {
    System.out.println("***************************************************************");
    System.out.println("*** seekBook request (Request-Response operation) is received *");
    System.out.println("***************************************************************");
    showSeekBookRequest(body);
    List<String> authorsLastNames = body.getAuthorLastName();
    if (authorsLastNames != null && authorsLastNames.size() > 0) {
        String authorsLastName = authorsLastNames.get(0);
        if (authorsLastName != null && authorsLastName.length() > 0 && (!"Icebear".equalsIgnoreCase(authorsLastName)) && (!"Morillo".equalsIgnoreCase(authorsLastName))) {
            SeekBookError e = prepareException("No book available from author " + authorsLastName);
            System.out.println("No book available from author " + authorsLastName);
            System.out.println("\nSending business fault (SeekBook error) with parameters:");
            Utils.showSeekBookError(e);
            throw e;
        }
    }
    ListOfBooks result = new ListOfBooks();
    if (authorsLastNames.contains("Icebear")) {
        BookType book = new BookType();
        result.getBook().add(book);
        PersonType author = new PersonType();
        book.getAuthor().add(author);
        author.setFirstName("Jack");
        author.setLastName("Icebear");
        Calendar dateOfBirth = new GregorianCalendar(101, Calendar.JANUARY, 2);
        author.setDateOfBirth(dateOfBirth.getTime());
        book.getTitle().add("Survival in the Arctic");
        book.getPublisher().add("Frosty Edition");
        book.setYearPublished("2010");
    }
    if (authorsLastNames.contains("Morillo")) {
        BookType book = new BookType();
        result.getBook().add(book);
        PersonType author = new PersonType();
        book.getAuthor().add(author);
        author.setFirstName("David A.");
        author.setLastName("Morillo");
        Calendar dateOfBirth = new GregorianCalendar(1970, Calendar.JANUARY, 1);
        author.setDateOfBirth(dateOfBirth.getTime());
        book.getTitle().add("The book about software");
        book.getPublisher().add("Frosty Edition");
        book.setYearPublished("2006");
    }
    System.out.println("Book(s) is found:");
    showSeekBookResponse(result);
    return result;
}
Also used : BookType(org.talend.types.demos.library.common._1.BookType) SeekBookError(org.talend.services.demos.library._1_0.SeekBookError) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) PersonType(org.talend.types.demos.library.common._1.PersonType) ListOfBooks(org.talend.types.demos.library.common._1.ListOfBooks)

Example 35 with Result

use of oasis.names.tc.dss._1_0.core.schema.Result in project open-ecard by ecsec.

the class WSHelper method makeResult.

public static Result makeResult(String major, String minor, String message, String lang) {
    Result r = new Result();
    r.setResultMajor(major);
    r.setResultMinor(minor);
    if (message != null) {
        InternationalStringType msg = new InternationalStringType();
        msg.setValue(message);
        msg.setLang(lang);
        r.setResultMessage(msg);
    }
    return r;
}
Also used : InternationalStringType(oasis.names.tc.dss._1_0.core.schema.InternationalStringType) Result(oasis.names.tc.dss._1_0.core.schema.Result)

Aggregations

Result (oasis.names.tc.dss._1_0.core.schema.Result)42 InternationalStringType (oasis.names.tc.dss._1_0.core.schema.InternationalStringType)12 SCIOException (org.openecard.common.ifd.scio.SCIOException)11 SingleThreadChannel (org.openecard.ifd.scio.wrapper.SingleThreadChannel)11 DIDAuthenticateResponse (iso.std.iso_iec._24727.tech.schema.DIDAuthenticateResponse)9 BigInteger (java.math.BigInteger)8 ThreadTerminateException (org.openecard.common.ThreadTerminateException)8 Test (org.testng.annotations.Test)8 Document (org.w3c.dom.Document)8 Calendar (java.util.Calendar)7 GregorianCalendar (java.util.GregorianCalendar)7 ExecutionException (java.util.concurrent.ExecutionException)7 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)6 TransmitResponse (iso.std.iso_iec._24727.tech.schema.TransmitResponse)6 InitializeFrameworkResponse (de.bund.bsi.ecard.api._1.InitializeFrameworkResponse)4 BeginTransactionResponse (iso.std.iso_iec._24727.tech.schema.BeginTransactionResponse)4 DIDAuthenticate (iso.std.iso_iec._24727.tech.schema.DIDAuthenticate)4 GetIFDCapabilitiesResponse (iso.std.iso_iec._24727.tech.schema.GetIFDCapabilitiesResponse)4 Transmit (iso.std.iso_iec._24727.tech.schema.Transmit)4 IOException (java.io.IOException)4