fix(pki): align JNI signatures between Kotlin and Rust
initLogging now takes logDir param matching Rust entry point. dumpLogs takes logDir+baseDir params matching Rust. Removed unused generateSoftwareKeyPair declaration. Added buffer bounds checks in parseNativeResult to prevent OOM on malformed native output.
This commit is contained in:
@@ -49,6 +49,9 @@ data class CertGenConfig(
|
|||||||
|
|
||||||
object NativeCertGen {
|
object NativeCertGen {
|
||||||
|
|
||||||
|
private const val LOG_DIR = "/data/adb/tricky_store/logs"
|
||||||
|
private const val BASE_DIR = "/data/adb/tricky_store"
|
||||||
|
|
||||||
@Volatile
|
@Volatile
|
||||||
var isAvailable: Boolean = false
|
var isAvailable: Boolean = false
|
||||||
private set
|
private set
|
||||||
@@ -56,6 +59,7 @@ object NativeCertGen {
|
|||||||
fun initialize(libraryPath: String) {
|
fun initialize(libraryPath: String) {
|
||||||
try {
|
try {
|
||||||
System.load(libraryPath)
|
System.load(libraryPath)
|
||||||
|
initLogging(false, LOG_DIR)
|
||||||
isAvailable = true
|
isAvailable = true
|
||||||
SystemLogger.info("NativeCertGen: loaded libcertgen.so successfully")
|
SystemLogger.info("NativeCertGen: loaded libcertgen.so successfully")
|
||||||
} catch (e: UnsatisfiedLinkError) {
|
} catch (e: UnsatisfiedLinkError) {
|
||||||
@@ -65,34 +69,42 @@ object NativeCertGen {
|
|||||||
|
|
||||||
external fun generateAttestedKeyPair(config: CertGenConfig): ByteArray?
|
external fun generateAttestedKeyPair(config: CertGenConfig): ByteArray?
|
||||||
|
|
||||||
external fun generateSoftwareKeyPair(
|
private external fun initLogging(verbose: Boolean, logDir: String): Boolean
|
||||||
algorithm: Int,
|
|
||||||
keySize: Int,
|
|
||||||
ecCurve: Int,
|
|
||||||
rsaPublicExponent: Long,
|
|
||||||
): ByteArray?
|
|
||||||
|
|
||||||
external fun initLogging(verbose: Boolean)
|
private external fun dumpLogs(logDir: String, baseDir: String): String?
|
||||||
|
|
||||||
external fun dumpLogs(): String
|
fun dump(): String? = if (isAvailable) dumpLogs(LOG_DIR, BASE_DIR) else null
|
||||||
|
|
||||||
fun parseNativeResult(bytes: ByteArray): Pair<KeyPair, List<Certificate>> {
|
fun parseNativeResult(bytes: ByteArray): Pair<KeyPair, List<Certificate>> {
|
||||||
val buf = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN)
|
val buf = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN)
|
||||||
|
|
||||||
val pkLen = buf.getInt()
|
val pkLen = buf.getInt()
|
||||||
|
if (pkLen < 0 || pkLen > buf.remaining()) {
|
||||||
|
throw IllegalStateException("Invalid private key length: $pkLen")
|
||||||
|
}
|
||||||
val pkBytes = ByteArray(pkLen)
|
val pkBytes = ByteArray(pkLen)
|
||||||
buf.get(pkBytes)
|
buf.get(pkBytes)
|
||||||
|
|
||||||
val numCerts = buf.getInt()
|
val numCerts = buf.getInt()
|
||||||
|
if (numCerts < 0 || numCerts > buf.remaining()) {
|
||||||
|
throw IllegalStateException("Invalid cert count: $numCerts")
|
||||||
|
}
|
||||||
val certs = mutableListOf<Certificate>()
|
val certs = mutableListOf<Certificate>()
|
||||||
val certFactory = CertificateFactory.getInstance("X.509")
|
val certFactory = CertificateFactory.getInstance("X.509")
|
||||||
repeat(numCerts) {
|
repeat(numCerts) {
|
||||||
val certLen = buf.getInt()
|
val certLen = buf.getInt()
|
||||||
|
if (certLen < 0 || certLen > buf.remaining()) {
|
||||||
|
throw IllegalStateException("Invalid cert length: $certLen")
|
||||||
|
}
|
||||||
val certBytes = ByteArray(certLen)
|
val certBytes = ByteArray(certLen)
|
||||||
buf.get(certBytes)
|
buf.get(certBytes)
|
||||||
certs.add(certFactory.generateCertificate(ByteArrayInputStream(certBytes)))
|
certs.add(certFactory.generateCertificate(ByteArrayInputStream(certBytes)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (certs.isEmpty()) {
|
||||||
|
throw IllegalStateException("No certificates in native result")
|
||||||
|
}
|
||||||
|
|
||||||
val algorithmName = when (certs[0].publicKey.algorithm) {
|
val algorithmName = when (certs[0].publicKey.algorithm) {
|
||||||
"EC" -> "EC"
|
"EC" -> "EC"
|
||||||
"RSA" -> "RSA"
|
"RSA" -> "RSA"
|
||||||
|
|||||||
Reference in New Issue
Block a user