Program: 8307
Project: 8307
Buggy source:
private Intent decryptAndVerifyImpl(Intent data,ParcelFileDescriptor inputDescriptor,ParcelFileDescriptor output,boolean decryptMetadataOnly){
  InputStream inputStream=null;
  OutputStream outputStream=null;
  try {
    inputStream=new ParcelFileDescriptor.AutoCloseInputStream(inputDescriptor);
    if (decryptMetadataOnly || output == null) {
      outputStream=null;
    }
 else {
      outputStream=new ParcelFileDescriptor.AutoCloseOutputStream(output);
    }
    String currentPkg=getCurrentCallingPackage();
    HashSet<Long> allowedKeyIds=mProviderHelper.getAllowedKeyIdsForApp(KeychainContract.ApiAllowedKeys.buildBaseUri(currentPkg));
    if (data.getIntExtra(OpenPgpApi.EXTRA_API_VERSION,-1) < 7) {
      allowedKeyIds.addAll(mProviderHelper.getAllKeyIdsForApp(ApiAccounts.buildBaseUri(currentPkg)));
    }
    CryptoInputParcel cryptoInput=CryptoInputParcelCacheService.getCryptoInputParcel(this,data);
    if (cryptoInput == null) {
      cryptoInput=new CryptoInputParcel();
    }
    if (data.hasExtra(OpenPgpApi.EXTRA_PASSPHRASE)) {
      cryptoInput=new CryptoInputParcel(cryptoInput.getSignatureTime(),new Passphrase(data.getCharArrayExtra(OpenPgpApi.EXTRA_PASSPHRASE)));
    }
    byte[] detachedSignature=data.getByteArrayExtra(OpenPgpApi.EXTRA_DETACHED_SIGNATURE);
    PgpDecryptVerify op=new PgpDecryptVerify(this,mProviderHelper,null);
    long inputLength=inputStream.available();
    PgpDecryptVerifyInputParcel input=new PgpDecryptVerifyInputParcel().setAllowSymmetricDecryption(false).setAllowedKeyIds(allowedKeyIds).setDecryptMetadataOnly(decryptMetadataOnly).setDetachedSignature(detachedSignature);
    DecryptVerifyResult pgpResult=op.execute(input,cryptoInput,inputStream,outputStream);
    if (pgpResult.isPending()) {
      RequiredInputParcel requiredInput=pgpResult.getRequiredInputParcel();
      PendingIntent pIntent=getRequiredInputPendingIntent(getBaseContext(),data,requiredInput);
      Intent result=new Intent();
      result.putExtra(OpenPgpApi.RESULT_INTENT,pIntent);
      result.putExtra(OpenPgpApi.RESULT_CODE,OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED);
      return result;
    }
 else     if (pgpResult.success()) {
      Intent result=new Intent();
      OpenPgpSignatureResult signatureResult=pgpResult.getSignatureResult();
      int resultType=OpenPgpApi.RESULT_TYPE_UNENCRYPTED_UNSIGNED;
      if (signatureResult != null) {
        resultType|=OpenPgpApi.RESULT_TYPE_SIGNED;
        if (!signatureResult.isSignatureOnly()) {
          resultType|=OpenPgpApi.RESULT_TYPE_ENCRYPTED;
        }
        result.putExtra(OpenPgpApi.RESULT_SIGNATURE,signatureResult);
        if (data.getIntExtra(OpenPgpApi.EXTRA_API_VERSION,-1) < 5) {
          if (signatureResult.getStatus() == OpenPgpSignatureResult.SIGNATURE_KEY_REVOKED || signatureResult.getStatus() == OpenPgpSignatureResult.SIGNATURE_KEY_EXPIRED) {
            signatureResult.setStatus(OpenPgpSignatureResult.SIGNATURE_ERROR);
          }
        }
        if (signatureResult.getStatus() == OpenPgpSignatureResult.SIGNATURE_KEY_MISSING) {
          result.putExtra(OpenPgpApi.RESULT_INTENT,getKeyserverPendingIntent(data,signatureResult.getKeyId()));
        }
 else {
          result.putExtra(OpenPgpApi.RESULT_INTENT,getShowKeyPendingIntent(signatureResult.getKeyId()));
        }
      }
 else {
        resultType|=OpenPgpApi.RESULT_TYPE_ENCRYPTED;
      }
      result.putExtra(OpenPgpApi.RESULT_TYPE,resultType);
      if (data.getIntExtra(OpenPgpApi.EXTRA_API_VERSION,-1) >= 4) {
        OpenPgpMetadata metadata=pgpResult.getDecryptMetadata();
        if (metadata != null) {
          result.putExtra(OpenPgpApi.RESULT_METADATA,metadata);
        }
      }
      String charset=pgpResult.getCharset();
      if (charset != null) {
        result.putExtra(OpenPgpApi.RESULT_CHARSET,charset);
      }
      result.putExtra(OpenPgpApi.RESULT_CODE,OpenPgpApi.RESULT_CODE_SUCCESS);
      return result;
    }
 else {
      if (pgpResult.isKeysDisallowed()) {
        Intent result=new Intent();
        result.putExtra(OpenPgpApi.RESULT_INTENT,getSelectAllowedKeysIntent(data));
        result.putExtra(OpenPgpApi.RESULT_CODE,OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED);
        return result;
      }
      String errorMsg=getString(pgpResult.getLog().getLast().mType.getMsgId());
      Intent result=new Intent();
      result.putExtra(OpenPgpApi.RESULT_ERROR,new OpenPgpError(OpenPgpError.GENERIC_ERROR,errorMsg));
      result.putExtra(OpenPgpApi.RESULT_CODE,OpenPgpApi.RESULT_CODE_ERROR);
      return result;
    }
  }
 catch (  IOException e) {
    Log.e(Constants.TAG,"String_Node_Str",e);
    Intent result=new Intent();
    result.putExtra(OpenPgpApi.RESULT_ERROR,new OpenPgpError(OpenPgpError.GENERIC_ERROR,e.getMessage()));
    result.putExtra(OpenPgpApi.RESULT_CODE,OpenPgpApi.RESULT_CODE_ERROR);
    return result;
  }
 finally {
    if (inputStream != null) {
      try {
        inputStream.close();
      }
 catch (      IOException e) {
        Log.e(Constants.TAG,"String_Node_Str",e);
      }
    }
    if (outputStream != null) {
      try {
        outputStream.close();
      }
 catch (      IOException e) {
        Log.e(Constants.TAG,"String_Node_Str",e);
      }
    }
  }
}

Fixed version (ground truth):
private Intent decryptAndVerifyImpl(Intent data,ParcelFileDescriptor inputDescriptor,ParcelFileDescriptor output,boolean decryptMetadataOnly){
  InputStream inputStream=null;
  OutputStream outputStream=null;
  try {
    inputStream=new ParcelFileDescriptor.AutoCloseInputStream(inputDescriptor);
    if (decryptMetadataOnly || output == null) {
      outputStream=null;
    }
 else {
      outputStream=new ParcelFileDescriptor.AutoCloseOutputStream(output);
    }
    String currentPkg=getCurrentCallingPackage();
    HashSet<Long> allowedKeyIds=mProviderHelper.getAllowedKeyIdsForApp(KeychainContract.ApiAllowedKeys.buildBaseUri(currentPkg));
    if (data.getIntExtra(OpenPgpApi.EXTRA_API_VERSION,-1) < 7) {
      allowedKeyIds.addAll(mProviderHelper.getAllKeyIdsForApp(ApiAccounts.buildBaseUri(currentPkg)));
    }
    CryptoInputParcel cryptoInput=CryptoInputParcelCacheService.getCryptoInputParcel(this,data);
    if (cryptoInput == null) {
      cryptoInput=new CryptoInputParcel();
    }
    if (data.hasExtra(OpenPgpApi.EXTRA_PASSPHRASE)) {
      cryptoInput=new CryptoInputParcel(cryptoInput.getSignatureTime(),new Passphrase(data.getCharArrayExtra(OpenPgpApi.EXTRA_PASSPHRASE)));
    }
    byte[] detachedSignature=data.getByteArrayExtra(OpenPgpApi.EXTRA_DETACHED_SIGNATURE);
    PgpDecryptVerify op=new PgpDecryptVerify(this,mProviderHelper,null);
    long inputLength=inputStream.available();
    InputData inputData=new InputData(inputStream,inputLength);
    PgpDecryptVerifyInputParcel input=new PgpDecryptVerifyInputParcel().setAllowSymmetricDecryption(false).setAllowedKeyIds(allowedKeyIds).setDecryptMetadataOnly(decryptMetadataOnly).setDetachedSignature(detachedSignature);
    DecryptVerifyResult pgpResult=op.execute(input,cryptoInput,inputData,outputStream);
    if (pgpResult.isPending()) {
      RequiredInputParcel requiredInput=pgpResult.getRequiredInputParcel();
      PendingIntent pIntent=getRequiredInputPendingIntent(getBaseContext(),data,requiredInput);
      Intent result=new Intent();
      result.putExtra(OpenPgpApi.RESULT_INTENT,pIntent);
      result.putExtra(OpenPgpApi.RESULT_CODE,OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED);
      return result;
    }
 else     if (pgpResult.success()) {
      Intent result=new Intent();
      OpenPgpSignatureResult signatureResult=pgpResult.getSignatureResult();
      int resultType=OpenPgpApi.RESULT_TYPE_UNENCRYPTED_UNSIGNED;
      if (signatureResult != null) {
        resultType|=OpenPgpApi.RESULT_TYPE_SIGNED;
        if (!signatureResult.isSignatureOnly()) {
          resultType|=OpenPgpApi.RESULT_TYPE_ENCRYPTED;
        }
        result.putExtra(OpenPgpApi.RESULT_SIGNATURE,signatureResult);
        if (data.getIntExtra(OpenPgpApi.EXTRA_API_VERSION,-1) < 5) {
          if (signatureResult.getStatus() == OpenPgpSignatureResult.SIGNATURE_KEY_REVOKED || signatureResult.getStatus() == OpenPgpSignatureResult.SIGNATURE_KEY_EXPIRED) {
            signatureResult.setStatus(OpenPgpSignatureResult.SIGNATURE_ERROR);
          }
        }
        if (signatureResult.getStatus() == OpenPgpSignatureResult.SIGNATURE_KEY_MISSING) {
          result.putExtra(OpenPgpApi.RESULT_INTENT,getKeyserverPendingIntent(data,signatureResult.getKeyId()));
        }
 else {
          result.putExtra(OpenPgpApi.RESULT_INTENT,getShowKeyPendingIntent(signatureResult.getKeyId()));
        }
      }
 else {
        resultType|=OpenPgpApi.RESULT_TYPE_ENCRYPTED;
      }
      result.putExtra(OpenPgpApi.RESULT_TYPE,resultType);
      if (data.getIntExtra(OpenPgpApi.EXTRA_API_VERSION,-1) >= 4) {
        OpenPgpMetadata metadata=pgpResult.getDecryptMetadata();
        if (metadata != null) {
          result.putExtra(OpenPgpApi.RESULT_METADATA,metadata);
        }
      }
      String charset=pgpResult.getCharset();
      if (charset != null) {
        result.putExtra(OpenPgpApi.RESULT_CHARSET,charset);
      }
      result.putExtra(OpenPgpApi.RESULT_CODE,OpenPgpApi.RESULT_CODE_SUCCESS);
      return result;
    }
 else {
      if (pgpResult.isKeysDisallowed()) {
        Intent result=new Intent();
        result.putExtra(OpenPgpApi.RESULT_INTENT,getSelectAllowedKeysIntent(data));
        result.putExtra(OpenPgpApi.RESULT_CODE,OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED);
        return result;
      }
      String errorMsg=getString(pgpResult.getLog().getLast().mType.getMsgId());
      Intent result=new Intent();
      result.putExtra(OpenPgpApi.RESULT_ERROR,new OpenPgpError(OpenPgpError.GENERIC_ERROR,errorMsg));
      result.putExtra(OpenPgpApi.RESULT_CODE,OpenPgpApi.RESULT_CODE_ERROR);
      return result;
    }
  }
 catch (  IOException e) {
    Log.e(Constants.TAG,"String_Node_Str",e);
    Intent result=new Intent();
    result.putExtra(OpenPgpApi.RESULT_ERROR,new OpenPgpError(OpenPgpError.GENERIC_ERROR,e.getMessage()));
    result.putExtra(OpenPgpApi.RESULT_CODE,OpenPgpApi.RESULT_CODE_ERROR);
    return result;
  }
 finally {
    if (inputStream != null) {
      try {
        inputStream.close();
      }
 catch (      IOException e) {
        Log.e(Constants.TAG,"String_Node_Str",e);
      }
    }
    if (outputStream != null) {
      try {
        outputStream.close();
      }
 catch (      IOException e) {
        Log.e(Constants.TAG,"String_Node_Str",e);
      }
    }
  }
}