Program: 109694
Project: 109694
Buggy source:
/** 
 * Downloads a new OpenVPN certificate, attaching authenticated cookie for authenticated certificate.
 * @param task containing the type of the certificate to be downloaded
 * @return true if certificate was downloaded correctly, false if provider.json or danger_on flag are not present in SharedPreferences, or if the certificate url could not be parsed as a URI, or if there was an SSL error. 
 */
private boolean getNewCert(Bundle task){
  try {
    String type_of_certificate=task.getString(ConfigurationWizard.TYPE_OF_CERTIFICATE);
    JSONObject provider_json=ConfigHelper.getJsonFromSharedPref(Provider.KEY);
    String provider_main_url=provider_json.getString(Provider.API_URL);
    URL new_cert_string_url=new URL(provider_main_url + "String_Node_Str" + provider_json.getString(Provider.API_VERSION)+ "String_Node_Str"+ EIP.CERTIFICATE);
    boolean danger_on=ConfigHelper.getBoolFromSharedPref(ProviderItem.DANGER_ON);
    String cert_string=downloadWithProviderCA(new_cert_string_url,danger_on);
    if (!cert_string.isEmpty()) {
      if (ConfigHelper.checkErroneousDownload(cert_string)) {
        String reason_to_fail=provider_json.getString(ERRORS);
        return false;
      }
 else {
        String certificateString=null, keyString=null;
        String[] certAndKey=cert_string.split("String_Node_Str");
        for (int i=0; i < certAndKey.length - 1; i++) {
          if (certAndKey[i].contains("String_Node_Str")) {
            keyString=certAndKey[i++] + certAndKey[i];
          }
 else           if (certAndKey[i].contains("String_Node_Str")) {
            certificateString=certAndKey[i++] + certAndKey[i];
          }
        }
        try {
          RSAPrivateKey keyCert=ConfigHelper.parseRsaKeyFromString(keyString);
          keyString=Base64.encodeToString(keyCert.getEncoded(),Base64.DEFAULT);
          ConfigHelper.saveSharedPref(EIP.PRIVATE_KEY,"String_Node_Str" + keyString + "String_Node_Str");
          X509Certificate certCert=ConfigHelper.parseX509CertificateFromString(certificateString);
          certificateString=Base64.encodeToString(certCert.getEncoded(),Base64.DEFAULT);
          ConfigHelper.saveSharedPref(EIP.CERTIFICATE,"String_Node_Str" + certificateString + "String_Node_Str");
          return true;
        }
 catch (        CertificateException e) {
          e.printStackTrace();
          return false;
        }
      }
    }
 else {
      return false;
    }
  }
 catch (  IOException e) {
    e.printStackTrace();
    return false;
  }
catch (  JSONException e) {
    e.printStackTrace();
    return false;
  }
}

Fixed version (ground truth):
/** 
 * Downloads a new OpenVPN certificate, attaching authenticated cookie for authenticated certificate.
 * @param task containing the type of the certificate to be downloaded
 * @return true if certificate was downloaded correctly, false if provider.json or danger_on flag are not present in SharedPreferences, or if the certificate url could not be parsed as a URI, or if there was an SSL error. 
 */
private boolean getNewCert(Bundle task){
  try {
    String type_of_certificate=task.getString(ConfigurationWizard.TYPE_OF_CERTIFICATE);
    JSONObject provider_json=ConfigHelper.getJsonFromSharedPref(Provider.KEY);
    String provider_main_url=provider_json.getString(Provider.API_URL);
    URL new_cert_string_url=new URL(provider_main_url + "String_Node_Str" + provider_json.getString(Provider.API_VERSION)+ "String_Node_Str"+ EIP.CERTIFICATE);
    boolean danger_on=ConfigHelper.getBoolFromSharedPref(ProviderItem.DANGER_ON);
    String cert_string=downloadWithProviderCA(new_cert_string_url.toString(),danger_on);
    if (!cert_string.isEmpty()) {
      if (ConfigHelper.checkErroneousDownload(cert_string)) {
        String reason_to_fail=provider_json.getString(ERRORS);
        return false;
      }
 else {
        String certificateString=null, keyString=null;
        String[] certAndKey=cert_string.split("String_Node_Str");
        for (int i=0; i < certAndKey.length - 1; i++) {
          if (certAndKey[i].contains("String_Node_Str")) {
            keyString=certAndKey[i++] + certAndKey[i];
          }
 else           if (certAndKey[i].contains("String_Node_Str")) {
            certificateString=certAndKey[i++] + certAndKey[i];
          }
        }
        try {
          RSAPrivateKey keyCert=ConfigHelper.parseRsaKeyFromString(keyString);
          keyString=Base64.encodeToString(keyCert.getEncoded(),Base64.DEFAULT);
          ConfigHelper.saveSharedPref(EIP.PRIVATE_KEY,"String_Node_Str" + keyString + "String_Node_Str");
          X509Certificate certCert=ConfigHelper.parseX509CertificateFromString(certificateString);
          certificateString=Base64.encodeToString(certCert.getEncoded(),Base64.DEFAULT);
          ConfigHelper.saveSharedPref(EIP.CERTIFICATE,"String_Node_Str" + certificateString + "String_Node_Str");
          return true;
        }
 catch (        CertificateException e) {
          e.printStackTrace();
          return false;
        }
      }
    }
 else {
      return false;
    }
  }
 catch (  IOException e) {
    e.printStackTrace();
    return false;
  }
catch (  JSONException e) {
    e.printStackTrace();
    return false;
  }
}