Skip to content

Commit

Permalink
Add dialog to input passphrase for encrypted SSH keys
Browse files Browse the repository at this point in the history
  • Loading branch information
hellfyre committed May 4, 2017
1 parent 6815890 commit b1eb12d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package org.stratum0.stratumstatusapp;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.EditText;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.KeyPair;
import com.jcraft.jsch.Session;

import java.io.ByteArrayOutputStream;
Expand All @@ -23,38 +27,52 @@ public class SSHConnectTask extends AsyncTask <String, String, String> {

private ArrayList<SSHConnectListener> receiverList = new ArrayList<>();
private Context context;
private SharedPreferences prefs;
private JSch jsch;
private boolean dialogDone;

private byte[] privateKey;
private byte[] privateKeyPassphrase;

public SSHConnectTask(Context context) {
this.context = context;
this.prefs = PreferenceManager.getDefaultSharedPreferences(this.context);
this.privateKey = this.prefs.getString("ssh_private_key", "").getBytes();
this.jsch = new JSch();
}

@Override
protected String doInBackground(String... strings) {

while (!this.dialogDone) {}

String operation = new String("close");
if (strings.length > 0 && !strings[0].isEmpty()) {
operation = strings[0].toLowerCase();
}

publishProgress(operation);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context);
String user = prefs.getString("ssh_user_" + operation, "zu");
String server = prefs.getString("ssh_server", "localhost");
String privateKey = prefs.getString("ssh_private_key", "");

JSch jsch = new JSch();
Session sshSession = null;
try {
sshSession = jsch.getSession(user, server);
NullUserInfo nui = new NullUserInfo();
sshSession.setUserInfo(nui);
sshSession = this.jsch.getSession(user, server);
NullUserInfo userInfo = new NullUserInfo();
sshSession.setUserInfo(userInfo);
} catch (JSchException e) {
e.printStackTrace();
}

try {
jsch.addIdentity("id_rsa", privateKey.getBytes(), null, null);
if (this.privateKeyPassphrase != null) {
Log.d(this.getClass().getName(), String.format("Using passphrase %s", this.privateKeyPassphrase.toString()));
}
else {
Log.d(this.getClass().getName(), "Passphrase is null");
}
this.jsch.addIdentity("id_rsa", this.privateKey, null, this.privateKeyPassphrase);
} catch (JSchException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -91,6 +109,46 @@ protected String doInBackground(String... strings) {
return baos.toString();
}

@Override
protected void onPreExecute() {
super.onPreExecute();

this.dialogDone = false;

try {
KeyPair keyPair = KeyPair.load(this.jsch, this.privateKey, null);

if (keyPair.isEncrypted()) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this.context);
alertDialogBuilder.setView(R.layout.dialog_edittext_passphrase);
alertDialogBuilder.setTitle(R.string.pref_privkey_dialog_title);
alertDialogBuilder.setMessage(R.string.pref_privkey_dialog_message);
alertDialogBuilder.setPositiveButton(this.context.getString(R.string.dialog_button_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AlertDialog alertDialog = (AlertDialog) dialog;
EditText passphrasefield = (EditText) alertDialog.findViewById(R.id.text_passphrase);
SSHConnectTask.this.privateKeyPassphrase = passphrasefield.getText().toString().getBytes();
SSHConnectTask.this.dialogDone = true;
}
}).setNegativeButton(this.context.getString(R.string.dialog_button_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SSHConnectTask.this.dialogDone = true;
}
});

alertDialogBuilder.create().show();
}
else {
this.privateKeyPassphrase = null;
this.dialogDone = true;
}
} catch (JSchException e) {
Log.e(this.getClass().getName(), e.getLocalizedMessage());
}
}

@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public enum Status {OPEN, CLOSED, UNKNOWN}
private SpaceStatus() {
status = Status.UNKNOWN;
openedBy = "";
lastUpdate = Calendar.getInstance();
lastChange = Calendar.getInstance();
since = Calendar.getInstance();
}
Expand Down
4 changes: 3 additions & 1 deletion mobile/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<string name="status_closed">Geschlossen</string>
<string name="status_unknown">Unbekannt</string>
<string name="text_confirm_generate_key">Wirklich einen neuen Schlüssel erzeugen?</string>
<string name="text_lastchange_label">Letzte Statusänderung:</string>
<string name="text_lastchange_label">Letzte Änderung:</string>
<string name="text_lastupdate_label">Letztes Update:</string>
<string name="text_openedby_label">Geöffnet von:</string>
<string name="text_since_label">Seit:</string>
Expand All @@ -34,4 +34,6 @@
<string name="pref_privkey_button_export_private_key_text">Privaten Schlüssel exportieren …</string>
<string name="pref_privkey_button_export_public_key_text">Öffentlichen Schlüssel exportieren …</string>
<string name="pref_privkey_button_import_private_key_text">Privaten Schlüssel importieren …</string>
<string name="pref_privkey_dialog_title">Privater Schlüssel</string>
<string name="pref_privkey_dialog_message">Passphrase für privaten Schlüssel eingeben</string>
</resources>
3 changes: 3 additions & 0 deletions mobile/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<string name="pref_privkey_button_import_private_key_text">Import private key …</string>
<string name="pref_privkey_button_generate_text">Generate private key</string>

<string name="pref_privkey_dialog_title">Private key</string>
<string name="pref_privkey_dialog_message">Enter passphrase for private key</string>

<!-- StatusActivity -->

<string name="button_open_title">Open</string>
Expand Down

0 comments on commit b1eb12d

Please sign in to comment.