Skip to content

Commit 29f2c30

Browse files
JingMatrixMhmRdd
andauthored
Align certificate validity and RSA defaults with AOSP (#167)
1. Certificate Validity: Matches `add_required_parameters`: - NotBefore: Unix Epoch (0). - NotAfter: RFC 5280 GeneralizedTime max (9999-12-31). Previous defaults (Current Time to +1 Year) imply a fingerprinting risk. 2. RSA Exponent: Added a null-safe fallback to F4 (65537). This prevents a NullPointerException when callers omit the public exponent parameter. Corresponding references: 1. https://cs.android.com/android/platform/superproject/+/android-latest-release:system/security/keystore2/src/security_level.rs; 2. https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/keystore/java/android/security/keystore2/AndroidKeyStoreKeyPairGeneratorSpi.java; Co-authored-by: Mohammed Riad <52679407+MhmRdd@users.noreply.github.com>
1 parent e82b982 commit 29f2c30

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

app/src/main/java/org/matrix/TEESimulator/pki/CertificateGenerator.kt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ import org.matrix.TEESimulator.logging.SystemLogger
3838
*/
3939
object CertificateGenerator {
4040

41+
// AOSP utils.rs: pub const UNDEFINED_NOT_AFTER: i64 = 253402300799000i64;
42+
// RFC 5280 GeneralizedTime maximum: 9999-12-31T23:59:59 UTC (millis since epoch)
43+
private const val UNDEFINED_NOT_AFTER = 253402300799000L
44+
4145
/**
4246
* Generates a software-based cryptographic key pair.
4347
*
@@ -51,7 +55,10 @@ object CertificateGenerator {
5155
Algorithm.EC -> "EC" to ECGenParameterSpec(params.ecCurveName)
5256
Algorithm.RSA ->
5357
"RSA" to
54-
RSAKeyGenParameterSpec(params.keySize, params.rsaPublicExponent)
58+
RSAKeyGenParameterSpec(
59+
params.keySize,
60+
params.rsaPublicExponent ?: RSAKeyGenParameterSpec.F4,
61+
)
5562
else ->
5663
throw IllegalArgumentException(
5764
"Unsupported algorithm: ${params.algorithm}"
@@ -218,16 +225,19 @@ object CertificateGenerator {
218225
securityLevel: Int,
219226
): Certificate {
220227
val subject = params.certificateSubject ?: X500Name("CN=Android Keystore Key")
221-
val leafNotAfter =
222-
(signingKeyPair.public as? X509Certificate)?.notAfter
223-
?: Date(System.currentTimeMillis() + 31536000000L)
228+
229+
// AOSP add_required_parameters (security_level.rs) defaults:
230+
// CERTIFICATE_NOT_BEFORE = 0 (Unix epoch)
231+
// CERTIFICATE_NOT_AFTER = 253402300799000 (9999-12-31T23:59:59 UTC)
232+
val notBefore = params.certificateNotBefore ?: Date(0)
233+
val notAfter = params.certificateNotAfter ?: Date(UNDEFINED_NOT_AFTER)
224234

225235
val builder =
226236
JcaX509v3CertificateBuilder(
227237
issuer,
228238
params.certificateSerial ?: BigInteger.ONE,
229-
params.certificateNotBefore ?: Date(),
230-
params.certificateNotAfter ?: leafNotAfter,
239+
notBefore,
240+
notAfter,
231241
subject,
232242
subjectKeyPair.public,
233243
)

0 commit comments

Comments
 (0)