-
Notifications
You must be signed in to change notification settings - Fork 88
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
Conversion of package appverifier > readercertgen to Kotlin #397
Merged
davidz25
merged 2 commits into
openwallet-foundation-labs:master
from
keesgeluk:conversion2kotlin
Oct 30, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 0 additions & 113 deletions
113
appverifier/src/main/java/com/android/mdl/appreader/readercertgen/CertificateGenerator.java
This file was deleted.
Oops, something went wrong.
108 changes: 108 additions & 0 deletions
108
appverifier/src/main/java/com/android/mdl/appreader/readercertgen/CertificateGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.android.mdl.appreader.readercertgen | ||
|
||
import org.bouncycastle.asn1.ASN1ObjectIdentifier | ||
import org.bouncycastle.asn1.x500.X500Name | ||
import org.bouncycastle.asn1.x509.BasicConstraints | ||
import org.bouncycastle.asn1.x509.ExtendedKeyUsage | ||
import org.bouncycastle.asn1.x509.Extension | ||
import org.bouncycastle.asn1.x509.GeneralName | ||
import org.bouncycastle.asn1.x509.GeneralNames | ||
import org.bouncycastle.asn1.x509.KeyPurposeId | ||
import org.bouncycastle.asn1.x509.KeyUsage | ||
import org.bouncycastle.asn1.x509.SubjectKeyIdentifier | ||
import org.bouncycastle.cert.CertIOException | ||
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter | ||
import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils | ||
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder | ||
import org.bouncycastle.operator.OperatorCreationException | ||
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder | ||
import java.io.IOException | ||
import java.security.NoSuchAlgorithmException | ||
import java.security.cert.CertificateException | ||
import java.security.cert.X509Certificate | ||
import java.util.Optional | ||
|
||
object CertificateGenerator { | ||
private const val CRITICAL = true | ||
private const val NOT_CRITICAL = false | ||
@JvmStatic | ||
@Throws(CertIOException::class, CertificateException::class, OperatorCreationException::class) | ||
fun generateCertificate( | ||
data: DataMaterial, | ||
certMaterial: CertificateMaterial, | ||
keyMaterial: KeyMaterial | ||
): X509Certificate { | ||
val issuerCert: Optional<X509Certificate> = keyMaterial.issuerCertificate | ||
val subjectDN = X500Name(data.subjectDN) | ||
// doesn't work, get's reordered | ||
// issuerCert.isPresent() ? new X500Name(issuerCert.get().getSubjectX500Principal().getName()) : subjectDN; | ||
val issuerDN = X500Name(data.issuerDN) | ||
val contentSigner = | ||
JcaContentSignerBuilder(keyMaterial.signingAlgorithm).build(keyMaterial.signingKey) | ||
val certBuilder = JcaX509v3CertificateBuilder( | ||
issuerDN, | ||
certMaterial.serialNumber, | ||
certMaterial.startDate, certMaterial.endDate, | ||
subjectDN, | ||
keyMaterial.publicKey | ||
) | ||
|
||
|
||
// Extensions -------------------------- | ||
val jcaX509ExtensionUtils: JcaX509ExtensionUtils | ||
jcaX509ExtensionUtils = try { | ||
JcaX509ExtensionUtils() | ||
} catch (e: NoSuchAlgorithmException) { | ||
throw RuntimeException(e) | ||
} | ||
if (issuerCert.isPresent) { | ||
try { | ||
// adds 3 more fields, not present in other cert | ||
// AuthorityKeyIdentifier authorityKeyIdentifier = jcaX509ExtensionUtils.createAuthorityKeyIdentifier(issuerCert.get()); | ||
val authorityKeyIdentifier = | ||
jcaX509ExtensionUtils.createAuthorityKeyIdentifier(issuerCert.get().publicKey) | ||
certBuilder.addExtension( | ||
Extension.authorityKeyIdentifier, | ||
NOT_CRITICAL, | ||
authorityKeyIdentifier | ||
) | ||
} catch (e: IOException) { // CertificateEncodingException | | ||
throw RuntimeException(e) | ||
} | ||
} | ||
val subjectKeyIdentifier: SubjectKeyIdentifier = | ||
jcaX509ExtensionUtils.createSubjectKeyIdentifier(keyMaterial.publicKey) | ||
certBuilder.addExtension(Extension.subjectKeyIdentifier, NOT_CRITICAL, subjectKeyIdentifier) | ||
val keyUsage = KeyUsage(certMaterial.keyUsage) | ||
certBuilder.addExtension(Extension.keyUsage, CRITICAL, keyUsage) | ||
|
||
// IssuerAlternativeName | ||
val issuerAlternativeName: Optional<String> = data.issuerAlternativeName | ||
if (issuerAlternativeName.isPresent) { | ||
val issuerAltName = GeneralNames( | ||
GeneralName( | ||
GeneralName.uniformResourceIdentifier, | ||
issuerAlternativeName.get() | ||
) | ||
) | ||
certBuilder.addExtension(Extension.issuerAlternativeName, NOT_CRITICAL, issuerAltName) | ||
} | ||
|
||
// Basic Constraints | ||
val pathLengthConstraint: Int = certMaterial.pathLengthConstraint | ||
if (pathLengthConstraint != CertificateMaterial.PATHLENGTH_NOT_A_CA) { | ||
// TODO doesn't work for certificate chains != 2 in size | ||
val basicConstraints = BasicConstraints(pathLengthConstraint) | ||
certBuilder.addExtension(Extension.basicConstraints, CRITICAL, basicConstraints) | ||
} | ||
val extendedKeyUsage: Optional<String> = certMaterial.extendedKeyUsage | ||
if (extendedKeyUsage.isPresent) { | ||
val keyPurpose = KeyPurposeId.getInstance(ASN1ObjectIdentifier(extendedKeyUsage.get())) | ||
val extKeyUsage = ExtendedKeyUsage(arrayOf(keyPurpose)) | ||
certBuilder.addExtension(Extension.extendedKeyUsage, CRITICAL, extKeyUsage) | ||
} | ||
|
||
// DEBUG setProvider(bcProvider) removed before getCertificate | ||
return JcaX509CertificateConverter().getCertificate(certBuilder.build(contentSigner)) | ||
} | ||
} |
21 changes: 0 additions & 21 deletions
21
appverifier/src/main/java/com/android/mdl/appreader/readercertgen/CertificateMaterial.java
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
appverifier/src/main/java/com/android/mdl/appreader/readercertgen/CertificateMaterial.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.android.mdl.appreader.readercertgen | ||
|
||
import java.math.BigInteger | ||
import java.util.Date | ||
import java.util.Optional | ||
|
||
data class CertificateMaterial( | ||
val serialNumber: BigInteger, | ||
val startDate: Date, | ||
val endDate: Date, | ||
val keyUsage: Int, | ||
val extendedKeyUsage: Optional<String>, | ||
val pathLengthConstraint: Int | ||
) { | ||
companion object { | ||
const val PATHLENGTH_NOT_A_CA = -1 | ||
} | ||
} |
12 changes: 0 additions & 12 deletions
12
appverifier/src/main/java/com/android/mdl/appreader/readercertgen/DataMaterial.java
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
appverifier/src/main/java/com/android/mdl/appreader/readercertgen/DataMaterial.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.android.mdl.appreader.readercertgen | ||
|
||
import java.util.Optional | ||
|
||
data class DataMaterial( | ||
val subjectDN: String, | ||
val issuerDN: String, | ||
val issuerAlternativeName: Optional<String> | ||
) |
24 changes: 0 additions & 24 deletions
24
appverifier/src/main/java/com/android/mdl/appreader/readercertgen/EncodingUtil.java
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
appverifier/src/main/java/com/android/mdl/appreader/readercertgen/EncodingUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.android.mdl.appreader.readercertgen | ||
|
||
import java.text.ParseException | ||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
import java.util.Locale | ||
|
||
object EncodingUtil { | ||
private val SHORT_ISO_DATEFORMAT = SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH) | ||
fun parseShortISODate(date: String): Date { | ||
return try { | ||
SHORT_ISO_DATEFORMAT.parse(date)!! | ||
} catch (e: ParseException) { | ||
throw RuntimeException(e) | ||
} | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
appverifier/src/main/java/com/android/mdl/appreader/readercertgen/KeyMaterial.java
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
appverifier/src/main/java/com/android/mdl/appreader/readercertgen/KeyMaterial.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.android.mdl.appreader.readercertgen | ||
|
||
import java.security.PrivateKey | ||
import java.security.PublicKey | ||
import java.security.cert.X509Certificate | ||
import java.util.Optional | ||
|
||
data class KeyMaterial( | ||
val publicKey: PublicKey, | ||
val signingAlgorithm: String, | ||
val issuerCertificate: Optional<X509Certificate>, | ||
val signingKey: PrivateKey | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be defined into a single expression, like so:
or even