enhancement: support PropertyDescriptor; support flashing vbmeta.img
parent
ec00936463
commit
1f7476d884
@ -0,0 +1,47 @@
|
||||
package cfig
|
||||
|
||||
import org.bouncycastle.asn1.pkcs.RSAPrivateKey
|
||||
import org.bouncycastle.util.io.pem.PemReader
|
||||
import java.io.InputStream
|
||||
import java.io.InputStreamReader
|
||||
import java.math.BigInteger
|
||||
import java.security.KeyFactory
|
||||
import java.security.PrivateKey
|
||||
import java.security.PublicKey
|
||||
import java.security.spec.PKCS8EncodedKeySpec
|
||||
import java.security.spec.RSAPrivateKeySpec
|
||||
import java.security.spec.RSAPublicKeySpec
|
||||
|
||||
class KeyUtil {
|
||||
companion object {
|
||||
@Throws(IllegalArgumentException::class)
|
||||
fun parsePemPrivateKey(inputStream: InputStream): RSAPrivateKey {
|
||||
val p = PemReader(InputStreamReader(inputStream)).readPemObject()
|
||||
if ("RSA PRIVATE KEY" != p.type) {
|
||||
throw IllegalArgumentException("input is not valid 'RSA PRIVATE KEY'")
|
||||
}
|
||||
return RSAPrivateKey.getInstance(p.content)
|
||||
}
|
||||
|
||||
fun parsePemPrivateKey2(inputStream: InputStream): PrivateKey {
|
||||
val rsa = KeyUtil.parsePemPrivateKey(inputStream)
|
||||
return generateRsaPrivateKey(rsa.modulus, rsa.privateExponent)
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun parsePk8PrivateKey(inputData: ByteArray): PrivateKey {
|
||||
val spec = PKCS8EncodedKeySpec(inputData)
|
||||
return KeyFactory.getInstance("RSA").generatePrivate(spec)
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
private fun generateRsaPublicKey(modulus: BigInteger, publicExponent: BigInteger): PublicKey {
|
||||
return KeyFactory.getInstance("RSA").generatePublic(RSAPublicKeySpec(modulus, publicExponent))
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
private fun generateRsaPrivateKey(modulus: BigInteger, privateExponent: BigInteger): PrivateKey {
|
||||
return KeyFactory.getInstance("RSA").generatePrivate(RSAPrivateKeySpec(modulus, privateExponent))
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package avb.desc
|
||||
|
||||
class ChainPartitionDescriptor {
|
||||
companion object {
|
||||
const val TAG = 4L
|
||||
const val RESERVED = 64
|
||||
const val SIZE = 28 + RESERVED
|
||||
const val FORMAT_STRING = "!2Q3L"
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
import avb.alg.Algorithms
|
||||
import cfig.KeyUtil
|
||||
import org.apache.commons.codec.binary.Hex
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import java.io.FileInputStream
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
|
||||
class KeyUtilTest {
|
||||
@Test
|
||||
fun parseKeys() {
|
||||
val keyFile = "../" + Algorithms.get("SHA256_RSA2048")!!.defaultKey
|
||||
val k = KeyUtil.parsePk8PrivateKey(Files.readAllBytes(Paths.get(keyFile.replace("pem", "pk8"))))
|
||||
|
||||
|
||||
val k2 = KeyUtil.parsePemPrivateKey2(FileInputStream(keyFile))
|
||||
println(Hex.encodeHexString(k.encoded))
|
||||
println(Hex.encodeHexString(k2.encoded))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue