Skip to content

Commit c379474

Browse files
WIP
1 parent 4230330 commit c379474

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

wallet-core/src/main/java/eu/europa/ec/eudi/wallet/keystore/KeyGenerator.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ package eu.europa.ec.eudi.wallet.keystore
33
import android.os.Build
44
import android.security.keystore.KeyGenParameterSpec
55
import android.security.keystore.KeyProperties
6+
import android.util.Base64
67
import androidx.annotation.RequiresApi
78
import eu.europa.ec.eudi.wallet.keystore.KeyGenerator.SigningKeyConfig
89
import java.io.IOException
910
import java.security.InvalidAlgorithmParameterException
11+
import java.security.InvalidKeyException
1012
import java.security.KeyPairGenerator
1113
import java.security.KeyStore
1214
import java.security.KeyStoreException
1315
import java.security.NoSuchAlgorithmException
1416
import java.security.NoSuchProviderException
17+
import java.security.PrivateKey
18+
import java.security.Signature
19+
import java.security.SignatureException
1520
import java.security.UnrecoverableEntryException
1621
import java.security.cert.CertificateException
1722

@@ -23,6 +28,9 @@ interface KeyGenerator {
2328
@Throws(KeyStoreException::class)
2429
fun getSigningKey(config: SigningKeyConfig): KeyStore.PrivateKeyEntry
2530

31+
@Throws(SignatureException::class)
32+
fun sign(key: PrivateKey, data: ByteArray): String
33+
2634
data class SigningKeyConfig(
2735
val keyType: Int,
2836
val timeoutSeconds: Int,
@@ -38,6 +46,32 @@ internal object KeyGeneratorImpl : KeyGenerator {
3846
return entry
3947
}
4048

49+
private const val SIGNATURE_ALGORITHM = "SHA256withECDSA"
50+
51+
@Throws(SignatureException::class)
52+
override fun sign(
53+
key: PrivateKey,
54+
data: ByteArray,
55+
) = try {
56+
Signature
57+
.getInstance(SIGNATURE_ALGORITHM)
58+
.run {
59+
initSign(key)
60+
update(data)
61+
sign()
62+
}.toBase64String()
63+
} catch (exception: NoSuchAlgorithmException) {
64+
throw SignatureException(exception)
65+
// throw SigningException("Signing failed.", exception)
66+
} catch (exception: InvalidKeyException) {
67+
throw SignatureException(exception)
68+
// throw SigningException("Signing failed.", exception)
69+
// } catch (exception: SignatureException) {
70+
// throw SigningException("Signing failed.", exception)
71+
}
72+
73+
private fun ByteArray.toBase64String() = String(Base64.encode(this, Base64.DEFAULT))
74+
4175
@RequiresApi(Build.VERSION_CODES.R)
4276
@Throws(KeyStoreException::class)
4377
private fun getKeyStoreEntry(config: SigningKeyConfig) = try {

wallet-core/src/main/java/eu/europa/ec/eudi/wallet/transfer/openid4vp/responseGenerator/OpenId4VpSdJwtResponseGeneratorImpl.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class OpenId4VpSdJwtResponseGeneratorImpl(
5555
val disclosedDocument = disclosedDocuments.documents.first()
5656

5757
val credentials = DocumentManagerSdJwt.getDocumentById(disclosedDocument.documentId)?.data
58-
?: throw IllegalArgumentException()
58+
?: throw IllegalArgumentException()
5959

6060
val sdJwt = getSdJwtFromCredentials(credentials)
6161

@@ -67,8 +67,8 @@ class OpenId4VpSdJwtResponseGeneratorImpl(
6767

6868
// val ecKey = ECKey.load(KeyGeneratorImpl.getKeyStore(), DEV_KEY_ALIAS, null)
6969

70-
val key= if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
71-
KeyGeneratorImpl.getSigningKey(KeyGenerator.SigningKeyConfig(KeyProperties.AUTH_DEVICE_CREDENTIAL,60))
70+
val key = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
71+
KeyGeneratorImpl.getSigningKey(KeyGenerator.SigningKeyConfig(KeyProperties.AUTH_DEVICE_CREDENTIAL, 60))
7272
} else {
7373
throw Exception()
7474
}
@@ -78,16 +78,21 @@ class OpenId4VpSdJwtResponseGeneratorImpl(
7878
certificateFactory.generateCertificate(ByteArrayInputStream(key.certificate.encoded)) as X509Certificate
7979
val ecKey = ECKey.parse(certificate)
8080

81-
val signer = ECDSASigner(ecKey)
81+
// val signer = ECDSASigner(ecKey)
8282

8383
val string = presentationSdJwt!!.serializeWithKeyBinding(
8484
jwtSerializer = { it.first },
8585
hashAlgorithm = HashAlgorithm.SHA_256,
8686
keyBindingSigner = object : KeyBindingSigner {
8787
override val signAlgorithm: JWSAlgorithm = JWSAlgorithm.ES256
8888
override val publicKey: AsymmetricJWK = ecKey.toPublicJWK()
89-
override fun getJCAContext(): JCAContext = signer.jcaContext
90-
override fun sign(p0: JWSHeader?, p1: ByteArray?): Base64URL = signer.sign(p0, p1)
89+
override fun getJCAContext(): JCAContext = JCAContext()
90+
@Throws(java.security.SignatureException::class)
91+
override fun sign(p0: JWSHeader?, p1: ByteArray?): Base64URL =
92+
Base64URL(KeyGeneratorImpl.sign(key.privateKey, p1 ?: ByteArray(0)))
93+
// override val publicKey: AsymmetricJWK = ecKey.toPublicJWK()
94+
// override fun getJCAContext(): JCAContext = signer.jcaContext
95+
// override fun sign(p0: JWSHeader?, p1: ByteArray?): Base64URL = signer.sign(p0, p1)
9196
},
9297
claimSetBuilderAction = { }
9398
)
@@ -176,8 +181,8 @@ class OpenId4VpSdJwtResponseGeneratorImpl(
176181
println(keyString)
177182

178183
val pemString = "-----BEGIN CERTIFICATE-----\n" +
179-
"${keyString}\n" +
180-
"-----END CERTIFICATE-----"
184+
"${keyString}\n" +
185+
"-----END CERTIFICATE-----"
181186

182187
val certificateFactory: CertificateFactory = CertificateFactory.getInstance("X.509")
183188
val certificate =

0 commit comments

Comments
 (0)