Skip to content

chore(phone-as-device): app-481 get private key from Keystore #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Certificates and a private key are stored in a device specific key store.
MqttClient.setIdentity({
caCertPem: IOT_CA_CERT, // PEM representation string of a root certificate
certPem: IOT_CERT, // PEM representation string of a client certificate
keyPem: IOT_KEY, // PEM representation string of a private key
keyTag: IOT_KEY, // key tag of the private key in Keystore/Keychain
keyStoreOptions, // options for a device specific key store. may be omitted
})
.then(() => {
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ android {
compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
buildToolsVersion getExtOrDefault('buildToolsVersion')
defaultConfig {
minSdkVersion 16
minSdkVersion 26
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
versionCode 1
versionName "1.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.github.emotokcak.reactnative.mqtt

import java.io.ByteArrayInputStream
import java.security.KeyFactory
import java.security.PrivateKey
import java.security.cert.CertificateFactory
import java.security.cert.X509Certificate
import java.security.spec.PKCS8EncodedKeySpec
import java.util.Base64

/** Utility to read a certificate and key from a PEM text. */
object PEMLoader {
Expand All @@ -28,40 +23,6 @@ object PEMLoader {
fun loadX509CertificateFromString(pem: String): X509Certificate {
val certificateFactory = CertificateFactory.getInstance("X.509")
return certificateFactory.generateCertificate(pem.byteInputStream())
as X509Certificate
}

/**
* Loads an RSA private key from a given PEM text.
*
* `pem` has to start with "-----BEGIN RSA PRIVATE KEY-----"
* and end with "-----END RSA PRIVATE KEY-----".
*
* @param pem
*
* PEM representation of an RSA private key.
*
* @throws InvalidKeySpecException
*
* If `pem` is invalid.
*
* @throws NoSuchAlgorithmException
*
* If the algorithm "RSA" is not supported.
*/
@JvmStatic
fun loadPrivateKeyFromString(pem: String): PrivateKey {
val isRSA = pem.startsWith("-----BEGIN RSA PRIVATE KEY-----")
val pemContents = pem
.replace("-----BEGIN RSA PRIVATE KEY-----", "")
.replace("-----END RSA PRIVATE KEY-----", "")
.replace("-----BEGIN EC PRIVATE KEY-----", "")
.replace("-----END EC PRIVATE KEY-----", "")
.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "")
val data = Base64.getMimeDecoder().decode(pemContents)
val keySpec = PKCS8EncodedKeySpec(data)
val keyFactory = KeyFactory.getInstance(if (isRSA) "RSA" else "EC")
return keyFactory.generatePrivate(keySpec)
as X509Certificate
}
}
Loading