2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-31 14:15:14 +00:00

Migrate RsaHelper to Kotlin

This commit is contained in:
TPJ Schikhof
2024-08-31 17:38:59 +00:00
committed by Albert Vaca Cintora
parent fb0e2cc01d
commit 0771415d8f
2 changed files with 83 additions and 85 deletions

View File

@@ -1,85 +0,0 @@
/*
* SPDX-FileCopyrightText: 2015 Albert Vaca Cintora <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect.Helpers.SecurityHelpers;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.security.keystore.KeyProperties;
import android.util.Base64;
import android.util.Log;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class RsaHelper {
public static void initialiseRsaKeys(Context context) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
if (!settings.contains("publicKey") || !settings.contains("privateKey")) {
KeyPair keyPair;
String keyAlgorithm;
try {
KeyPairGenerator keyGen;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
keyAlgorithm = KeyProperties.KEY_ALGORITHM_EC;
keyGen = KeyPairGenerator.getInstance(keyAlgorithm);
ECGenParameterSpec spec = new ECGenParameterSpec("secp256r1");
keyGen.initialize(spec);
} else {
keyAlgorithm = "RSA";
keyGen = KeyPairGenerator.getInstance(keyAlgorithm);
keyGen.initialize(2048);
}
keyPair = keyGen.generateKeyPair();
} catch (Exception e) {
Log.e("KDE/initializeRsaKeys", "Exception", e);
return;
}
byte[] publicKey = keyPair.getPublic().getEncoded();
byte[] privateKey = keyPair.getPrivate().getEncoded();
SharedPreferences.Editor edit = settings.edit();
edit.putString("publicKey", Base64.encodeToString(publicKey, 0).trim() + "\n");
edit.putString("privateKey", Base64.encodeToString(privateKey, 0));
edit.putString("keyAlgorithm", keyAlgorithm);
edit.apply();
}
}
public static PublicKey getPublicKey(Context context) throws GeneralSecurityException {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
byte[] publicKeyBytes = Base64.decode(settings.getString("publicKey", ""), 0);
// For backwards compat: if no keyAlgorithm setting is set, it means it was generated using RSA
String keyAlgorithm = settings.getString("keyAlgorithm", "RSA");
return KeyFactory.getInstance(keyAlgorithm).generatePublic(new X509EncodedKeySpec(publicKeyBytes));
}
public static PrivateKey getPrivateKey(Context context) throws GeneralSecurityException {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
byte[] privateKeyBytes = Base64.decode(settings.getString("privateKey", ""), 0);
// For backwards compat: if no keyAlgorithm setting is set, it means it was generated using RSA
String keyAlgorithm = settings.getString("keyAlgorithm", "RSA");
return KeyFactory.getInstance(keyAlgorithm).generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));
}
}

View File

@@ -0,0 +1,83 @@
/*
* SPDX-FileCopyrightText: 2015 Albert Vaca Cintora <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect.Helpers.SecurityHelpers
import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import android.preference.PreferenceManager
import android.security.keystore.KeyProperties
import android.util.Base64
import android.util.Log
import java.security.GeneralSecurityException
import java.security.KeyFactory
import java.security.KeyPair
import java.security.KeyPairGenerator
import java.security.PrivateKey
import java.security.PublicKey
import java.security.spec.ECGenParameterSpec
import java.security.spec.PKCS8EncodedKeySpec
import java.security.spec.X509EncodedKeySpec
object RsaHelper {
private const val RSA = "RSA" // KeyProperties.KEY_ALGORITHM_RSA isn't available until API 23+
@JvmStatic
fun initialiseRsaKeys(context: Context?) {
val settings = PreferenceManager.getDefaultSharedPreferences(context)
if (!settings.contains("publicKey") || !settings.contains("privateKey")) {
val keyPair: KeyPair
val keyAlgorithm: String
try {
val newSdk = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
keyAlgorithm = if (newSdk) KeyProperties.KEY_ALGORITHM_EC else RSA
val generator = KeyPairGenerator.getInstance(keyAlgorithm)
if (newSdk) {
val spec = ECGenParameterSpec("secp256r1")
generator.initialize(spec)
}
else {
generator.initialize(2048)
}
keyPair = generator.generateKeyPair()
}
catch (e: Exception) {
Log.e("KDE/initializeRsaKeys", "Exception", e)
return
}
val publicKey = keyPair.public.encoded
val privateKey = keyPair.private.encoded
settings.edit().apply {
putString("publicKey", Base64.encodeToString(publicKey, 0))
putString("privateKey", Base64.encodeToString(privateKey, 0))
putString("keyAlgorithm", keyAlgorithm)
apply()
}
}
}
/** For backwards compat: if no keyAlgorithm setting is set, it means it was generated using RSA */
private fun algorithmFromSettings(pref: SharedPreferences) = pref.getString("keyAlgorithm", RSA)
@JvmStatic
@Throws(GeneralSecurityException::class)
fun getPublicKey(context: Context?): PublicKey {
val settings = PreferenceManager.getDefaultSharedPreferences(context)
val publicKeyBytes = Base64.decode(settings.getString("publicKey", ""), 0)
return KeyFactory.getInstance(algorithmFromSettings(settings)).generatePublic(X509EncodedKeySpec(publicKeyBytes))
}
@JvmStatic
@Throws(GeneralSecurityException::class)
fun getPrivateKey(context: Context?): PrivateKey {
val settings = PreferenceManager.getDefaultSharedPreferences(context)
val privateKeyBytes = Base64.decode(settings.getString("privateKey", ""), 0)
return KeyFactory.getInstance(algorithmFromSettings(settings)).generatePrivate(PKCS8EncodedKeySpec(privateKeyBytes))
}
}