Program: 48144
Project: 48144
Buggy source:
/** 
 * 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 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("String_Node_Str");
  didList.setFilter(didQualifier);
  DIDListResponse didListResponse=instance.didList(didList);
  assertTrue(didListResponse.getDIDNameList().getDIDName().size() > 0);
  WSHelper.checkResult(didListResponse);
  DIDAuthenticate didAthenticate=new DIDAuthenticate();
  didAthenticate.setDIDName("String_Node_Str");
  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.common.sal.anytype.CryptoMarkerType cryptoMarker=new org.openecard.common.sal.anytype.CryptoMarkerType((CryptoMarkerType)didGetResponse.getDIDStructure().getDIDMarker());
    ByteArrayOutputStream ciphertext=new ByteArrayOutputStream();
    DSIRead dsiRead=new DSIRead();
    dsiRead.setConnectionHandle(result.getConnectionHandle());
    dsiRead.getConnectionHandle().setCardApplication(cardApplication);
    dsiRead.setDSIName(cryptoMarker.getCertificateRef().getDataSetName());
    DSIReadResponse dsiReadResponse=instance.dsiRead(dsiRead);
    assertEquals(ECardConstants.Major.OK,dsiReadResponse.getResult().getResultMajor());
    assertTrue(dsiReadResponse.getDSIContent().length > 0);
    Certificate cert=(X509Certificate)CertificateFactory.getInstance("String_Node_Str").generateCertificate(new ByteArrayInputStream(dsiReadResponse.getDSIContent()));
    Cipher cipher;
    int blocksize;
    String algorithmOID=cryptoMarker.getAlgorithmInfo().getAlgorithmIdentifier().getAlgorithm();
    if (algorithmOID.equals(GenericCryptoObjectIdentifier.rsaEncryption)) {
      cipher=Cipher.getInstance("String_Node_Str");
      cipher.init(Cipher.ENCRYPT_MODE,cert);
      blocksize=245;
    }
 else     if (algorithmOID.equals(GenericCryptoObjectIdentifier.id_RSAES_OAEP)) {
      cipher=Cipher.getInstance("String_Node_Str",new BouncyCastleProvider());
      cipher.init(Cipher.ENCRYPT_MODE,cert);
      blocksize=cipher.getBlockSize();
    }
 else {
      logger.warn("String_Node_Str",algorithmOID);
      continue;
    }
    int rest=plaintextBytes.length % blocksize;
    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);
    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);
  }
}

Fixed version (ground truth):
/** 
 * 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 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("String_Node_Str");
  didList.setFilter(didQualifier);
  DIDListResponse didListResponse=instance.didList(didList);
  assertTrue(didListResponse.getDIDNameList().getDIDName().size() > 0);
  WSHelper.checkResult(didListResponse);
  DIDAuthenticate didAthenticate=new DIDAuthenticate();
  didAthenticate.setDIDName("String_Node_Str");
  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.CryptoMarkerType cryptoMarker=new org.openecard.crypto.common.sal.CryptoMarkerType((CryptoMarkerType)didGetResponse.getDIDStructure().getDIDMarker());
    ByteArrayOutputStream ciphertext=new ByteArrayOutputStream();
    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);
    Certificate cert=(X509Certificate)CertificateFactory.getInstance("String_Node_Str").generateCertificate(new ByteArrayInputStream(dsiReadResponse.getDSIContent()));
    Cipher cipher;
    int blocksize;
    String algorithmOID=cryptoMarker.getAlgorithmInfo().getAlgorithmIdentifier().getAlgorithm();
    if (algorithmOID.equals(GenericCryptoObjectIdentifier.rsaEncryption)) {
      cipher=Cipher.getInstance("String_Node_Str");
      cipher.init(Cipher.ENCRYPT_MODE,cert);
      blocksize=245;
    }
 else     if (algorithmOID.equals(GenericCryptoObjectIdentifier.id_RSAES_OAEP)) {
      cipher=Cipher.getInstance("String_Node_Str",new BouncyCastleProvider());
      cipher.init(Cipher.ENCRYPT_MODE,cert);
      blocksize=cipher.getBlockSize();
    }
 else {
      logger.warn("String_Node_Str",algorithmOID);
      continue;
    }
    int rest=plaintextBytes.length % blocksize;
    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);
    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);
  }
}