mirror of
https://github.com/izzy2lost/MGit.git
synced 2026-03-10 12:32:58 -07:00
Update JSCH and Conscrypt
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package com.manichord.mgit.ssh
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.AlertDialog
|
||||
import android.app.Dialog
|
||||
import android.os.Bundle
|
||||
import android.widget.EditText
|
||||
import android.widget.RadioGroup
|
||||
import com.jcraft.jsch.JSch
|
||||
import com.jcraft.jsch.KeyPair
|
||||
import me.sheimi.android.views.SheimiDialogFragment
|
||||
import me.sheimi.sgit.R
|
||||
import me.sheimi.sgit.activities.explorer.PrivateKeyManageActivity
|
||||
import me.sheimi.sgit.ssh.PrivateKeyUtils
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
|
||||
class PrivateKeyGenerate : SheimiDialogFragment() {
|
||||
private lateinit var mNewFilename: EditText
|
||||
private lateinit var mKeyLength: EditText
|
||||
private lateinit var mRadioGroup: RadioGroup
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
// Use the Builder class for convenient dialog construction
|
||||
val builder = AlertDialog.Builder(activity)
|
||||
val inflater = requireActivity().layoutInflater
|
||||
val view = inflater.inflate(R.layout.dialog_generate_key, null)
|
||||
mNewFilename = view.findViewById(R.id.newFilename)
|
||||
mKeyLength = view.findViewById(R.id.key_size)
|
||||
mKeyLength.setText("4096")
|
||||
mRadioGroup = view.findViewById(R.id.radio_keygen_type)
|
||||
builder.setMessage(R.string.label_dialog_generate_key)
|
||||
.setView(view)
|
||||
.setPositiveButton(R.string.label_generate_key) { _, _ -> generateKey() }
|
||||
.setNegativeButton(R.string.label_cancel) { _, _ -> }
|
||||
return builder.create()
|
||||
}
|
||||
|
||||
private fun generateKey() {
|
||||
val newFilename = mNewFilename.text.toString().trim { it <= ' ' }
|
||||
if (newFilename == "") {
|
||||
showToastMessage(R.string.alert_new_filename_required)
|
||||
mNewFilename.error = getString(R.string.alert_new_filename_required)
|
||||
return
|
||||
}
|
||||
if (newFilename.contains("/")) {
|
||||
showToastMessage(R.string.alert_filename_format)
|
||||
mNewFilename.error = getString(R.string.alert_filename_format)
|
||||
return
|
||||
}
|
||||
val keySize = mKeyLength.text.toString().toInt()
|
||||
if (keySize < 1024) {
|
||||
showToastMessage(R.string.alert_too_short_key_size)
|
||||
mNewFilename.error = getString(R.string.alert_too_short_key_size)
|
||||
return
|
||||
}
|
||||
if (keySize > 16384) {
|
||||
showToastMessage(R.string.alert_too_long_key_size)
|
||||
mNewFilename.error = getString(R.string.alert_too_long_key_size)
|
||||
return
|
||||
}
|
||||
val type = when (mRadioGroup.checkedRadioButtonId) {
|
||||
R.id.radio_dsa -> KeyPair.DSA
|
||||
// JSCH doesn't support writing ED25519 keys yet, only reading
|
||||
//R.id.radio_ed25519 -> KeyPair.ED25519
|
||||
else -> KeyPair.RSA
|
||||
}
|
||||
val newKey = File(PrivateKeyUtils.getPrivateKeyFolder(), newFilename)
|
||||
val newPubKey = File(PrivateKeyUtils.getPublicKeyFolder(), newFilename)
|
||||
try {
|
||||
val jsch = JSch()
|
||||
val kpair = KeyPair.genKeyPair(jsch, type, keySize)
|
||||
kpair.writePrivateKey(FileOutputStream(newKey))
|
||||
kpair.writePublicKey(FileOutputStream(newPubKey), "mgit")
|
||||
kpair.dispose()
|
||||
} catch (e: Exception) {
|
||||
//TODO
|
||||
e.printStackTrace()
|
||||
}
|
||||
(activity as PrivateKeyManageActivity?)!!.refreshList()
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import javax.net.ssl.SSLSocketFactory;
|
||||
public class MGitSSLSocketFactory extends SSLSocketFactory {
|
||||
|
||||
private SSLSocketFactory wrappedSSLSocketFactory;
|
||||
public static String[] enabledProtocols = new String[] {"TLSv1.2", "TLSv1.1", "TLSv1"};
|
||||
public static String[] enabledProtocols = new String[] {"TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1"};
|
||||
|
||||
public MGitSSLSocketFactory(SSLSocketFactory wrapped) {
|
||||
wrappedSSLSocketFactory = wrapped;
|
||||
|
||||
@@ -10,8 +10,11 @@ import org.acra.config.dialog
|
||||
import org.acra.config.mailSender
|
||||
import org.acra.data.StringFormat
|
||||
import org.acra.ktx.initAcra
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider
|
||||
import org.conscrypt.Conscrypt
|
||||
import org.eclipse.jgit.transport.CredentialsProvider
|
||||
import timber.log.Timber
|
||||
import java.security.Security
|
||||
|
||||
/**
|
||||
* Custom Application Singleton
|
||||
@@ -24,7 +27,7 @@ open class MGitApplication : Application() {
|
||||
companion object {
|
||||
private lateinit var mContext: Context
|
||||
private lateinit var mCredentialsProvider: CredentialsProvider
|
||||
val context: Context?
|
||||
val context: Context
|
||||
get() = mContext
|
||||
|
||||
@JvmStatic fun getContext(): MGitApplication {
|
||||
@@ -37,6 +40,8 @@ open class MGitApplication : Application() {
|
||||
|
||||
init {
|
||||
MGitHttpConnectionFactory.install()
|
||||
Security.addProvider(BouncyCastleProvider())
|
||||
Security.addProvider(Conscrypt.newProvider())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
package me.sheimi.sgit.activities.explorer;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.RadioButton;
|
||||
|
||||
import com.jcraft.jsch.JSch;
|
||||
import com.jcraft.jsch.KeyPair;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
import me.sheimi.android.views.SheimiDialogFragment;
|
||||
import me.sheimi.sgit.R;
|
||||
import me.sheimi.sgit.ssh.PrivateKeyUtils;
|
||||
|
||||
public class PrivateKeyGenerate extends SheimiDialogFragment {
|
||||
|
||||
private EditText mNewFilename;
|
||||
private EditText mKeyLength;
|
||||
private RadioButton mDSAButton;
|
||||
private RadioButton mRSAButton;
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
// Use the Builder class for convenient dialog construction
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||
LayoutInflater inflater = getActivity().getLayoutInflater();
|
||||
View view;
|
||||
view = inflater.inflate(R.layout.dialog_generate_key, null);
|
||||
mNewFilename = (EditText) view.findViewById(R.id.newFilename);
|
||||
mKeyLength = (EditText) view.findViewById(R.id.key_size);
|
||||
mKeyLength.setText("4096");
|
||||
mDSAButton = (RadioButton) view.findViewById(R.id.radio_dsa);
|
||||
mRSAButton = (RadioButton) view.findViewById(R.id.radio_rsa);
|
||||
mRSAButton.setChecked(true);
|
||||
builder.setMessage(R.string.label_dialog_generate_key)
|
||||
.setView(view)
|
||||
.setPositiveButton(R.string.label_generate_key, new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
generateKey();
|
||||
}
|
||||
})
|
||||
.setNegativeButton(R.string.label_cancel, new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
// Nothing to do
|
||||
}
|
||||
});
|
||||
return builder.create();
|
||||
}
|
||||
|
||||
private void generateKey() {
|
||||
String newFilename = mNewFilename.getText().toString().trim();
|
||||
|
||||
if (newFilename.equals("")) {
|
||||
showToastMessage(R.string.alert_new_filename_required);
|
||||
mNewFilename
|
||||
.setError(getString(R.string.alert_new_filename_required));
|
||||
return;
|
||||
}
|
||||
|
||||
if (newFilename.contains("/")) {
|
||||
showToastMessage(R.string.alert_filename_format);
|
||||
mNewFilename.setError(getString(R.string.alert_filename_format));
|
||||
return;
|
||||
}
|
||||
|
||||
int key_size = Integer.parseInt(mKeyLength.getText().toString());
|
||||
|
||||
if (key_size < 1024) {
|
||||
showToastMessage(R.string.alert_too_short_key_size);
|
||||
mNewFilename.setError(getString(R.string.alert_too_short_key_size));
|
||||
return;
|
||||
}
|
||||
if (key_size > 16384) {
|
||||
showToastMessage(R.string.alert_too_long_key_size);
|
||||
mNewFilename.setError(getString(R.string.alert_too_long_key_size));
|
||||
return;
|
||||
}
|
||||
int type = mDSAButton.isChecked() ? KeyPair.DSA : KeyPair.RSA;
|
||||
File newKey = new File(PrivateKeyUtils.getPrivateKeyFolder(), newFilename);
|
||||
File newPubKey = new File(PrivateKeyUtils.getPublicKeyFolder(), newFilename);
|
||||
|
||||
try {
|
||||
JSch jsch=new JSch();
|
||||
KeyPair kpair=KeyPair.genKeyPair(jsch, type, key_size);
|
||||
kpair.writePrivateKey(new FileOutputStream(newKey));
|
||||
kpair.writePublicKey(new FileOutputStream(newPubKey), "sgit");
|
||||
kpair.dispose();
|
||||
} catch (Exception e) {
|
||||
//TODO
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
((PrivateKeyManageActivity)getActivity()).refreshList();
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,8 @@ import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
|
||||
import com.manichord.mgit.ssh.PrivateKeyGenerate;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
|
||||
@@ -88,21 +90,12 @@ public class PrivateKeyManageActivity extends FileExplorerActivity implements Ac
|
||||
startActivity(intent);
|
||||
return true;
|
||||
case R.id.action_mode_edit_key_password:
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
|
||||
new AlertDialog.Builder(this)
|
||||
.setIcon(android.R.drawable.ic_dialog_alert)
|
||||
.setTitle(R.string.dialog_not_supported)
|
||||
.setMessage(R.string.dialog_not_supported_msg)
|
||||
.setPositiveButton(R.string.label_ok, null)
|
||||
.show();
|
||||
} else {
|
||||
pathArg = new Bundle();
|
||||
pathArg.putString(EditKeyPasswordDialog.KEY_FILE_EXTRA, mChosenFile.getAbsolutePath());
|
||||
mode.finish();
|
||||
EditKeyPasswordDialog editDialog = new EditKeyPasswordDialog();
|
||||
editDialog.setArguments(pathArg);
|
||||
editDialog.show(getSupportFragmentManager(), "rename-dialog");
|
||||
}
|
||||
pathArg = new Bundle();
|
||||
pathArg.putString(EditKeyPasswordDialog.KEY_FILE_EXTRA, mChosenFile.getAbsolutePath());
|
||||
mode.finish();
|
||||
EditKeyPasswordDialog editDialog = new EditKeyPasswordDialog();
|
||||
editDialog.setArguments(pathArg);
|
||||
editDialog.show(getSupportFragmentManager(), "rename-dialog");
|
||||
return true;
|
||||
case R.id.action_mode_delete:
|
||||
mode.finish();
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
android:orientation="vertical"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/newFilename"
|
||||
android:inputType="text"
|
||||
@@ -12,6 +13,7 @@
|
||||
android:layout_marginRight="4dp"
|
||||
android:layout_marginBottom="4dp"
|
||||
android:hint="@string/label_new_file_name" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/key_size"
|
||||
android:inputType="number"
|
||||
@@ -22,18 +24,31 @@
|
||||
android:layout_marginRight="4dp"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:fontFamily="sans-serif"
|
||||
android:hint="@string/label_key_size"/>
|
||||
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
<RadioButton android:id="@+id/radio_rsa"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="RSA"/>
|
||||
<RadioButton android:id="@+id/radio_dsa"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="DSA"/>
|
||||
android:hint="@string/label_key_size" />
|
||||
|
||||
<RadioGroup
|
||||
android:id="@+id/radio_keygen_type"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/radio_rsa"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:checked="true"
|
||||
android:text="RSA" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/radio_dsa"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="DSA" />
|
||||
<!-- JSCH doesn't support writing ED25519 keys yet, only reading -->
|
||||
<!--<RadioButton
|
||||
android:id="@+id/radio_ed25519"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="ED25519" />-->
|
||||
</RadioGroup>
|
||||
</LinearLayout>
|
||||
|
||||
Reference in New Issue
Block a user