Skip to content

Commit

Permalink
Fix #54 - Support for ES384 during verification
Browse files Browse the repository at this point in the history
  • Loading branch information
josmilan authored Aug 30, 2024
1 parent 9ea9b43 commit 5c94971
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import com.nimbusds.jose.util.Base64URL
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.net.URL
import com.google.gson.JsonArray
import com.google.gson.JsonParser
import com.nimbusds.jose.JOSEException
import com.nimbusds.jose.JWSAlgorithm
import com.nimbusds.jose.JWSVerifier
Expand All @@ -43,10 +41,11 @@ class SignatureValidator {
val jwsObject = JWSObject.parse(jwt)
val header = jwsObject.header
val kid = header.keyID
val algorithm = jwsObject.header.algorithm

// Check the format of kid and process accordingly
val response = if ( kid !=null && kid.startsWith("did:key:z")) {
processJWKFromKID(kid)
processJWKFromKID(kid,algorithm)
} else if ( kid !=null && kid.startsWith("did:ebsi:z")){
processEbsiJWKFromKID(kid)
}
Expand Down Expand Up @@ -130,7 +129,7 @@ class SignatureValidator {
* @param did
* @return
*/
private fun processJWKFromKID(did: String?): JWK? {
private fun processJWKFromKID(did: String?, algorithm: JWSAlgorithm): JWK? {
try {
if (did == null || !did.startsWith("did:key:z")) {
throw IllegalArgumentException("Invalid DID format")
Expand All @@ -142,7 +141,7 @@ class SignatureValidator {
did.substring("did:key:z".length)
}
// Call convertDIDToJWK function from DIDService
return DIDService().convertDIDToJWK(multiBaseEncoded)
return DIDService().convertDIDToJWK(multiBaseEncoded,algorithm)
} catch (e: IllegalArgumentException) {
// Handle specific exception if needed
throw IllegalArgumentException("Error converting DID to JWK", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.ewc.eudi_wallet_oidc_android.services.did

import com.ewc.eudi_wallet_oidc_android.CryptographicAlgorithms
import com.mediaparkpk.base58android.Base58
import com.nimbusds.jose.JOSEException
import com.nimbusds.jose.JWSAlgorithm
import com.nimbusds.jose.crypto.ECDSAVerifier
import com.nimbusds.jose.jwk.Curve
import com.nimbusds.jose.jwk.ECKey
import com.nimbusds.jose.jwk.JWK
Expand Down Expand Up @@ -211,7 +214,7 @@ class DIDService : DIDServiceInterface {
* @return JWK object
* @throws IllegalArgumentException if the DID format is invalid, decoding fails, or JSON parsing errors occur
*/
override fun convertDIDToJWK(did: String): JWK {
override fun convertDIDToJWK(did: String, algorithm: JWSAlgorithm,): JWK {
val multiCodecBytes = try {
Base58.decode(did)
} catch (e: IllegalArgumentException) {
Expand All @@ -233,7 +236,13 @@ class DIDService : DIDServiceInterface {
val y = jsonObject.get("y") as String

// Create ECKey using Curve.P_256 (or appropriate curve)
val ecKey = ECKey.Builder(Curve.P_256, Base64URL.from(x), Base64URL.from(y))
val curve= when (algorithm) {
JWSAlgorithm.ES256 -> Curve.P_256
JWSAlgorithm.ES384 -> Curve.P_384
JWSAlgorithm.ES512 -> Curve.P_521
else -> throw JOSEException("Unsupported JWS algorithm $algorithm")
}
val ecKey = ECKey.Builder(curve, Base64URL.from(x), Base64URL.from(y))
.build()

// Return as JWK
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.ewc.eudi_wallet_oidc_android.services.did

import com.ewc.eudi_wallet_oidc_android.CryptographicAlgorithms
import com.nimbusds.jose.JWSAlgorithm
import com.nimbusds.jose.jwk.ECKey
import com.nimbusds.jose.jwk.JWK
import com.nimbusds.jose.jwk.OctetKeyPair
import com.nimbusds.jose.util.Base64URL

interface DIDServiceInterface {
Expand Down Expand Up @@ -84,5 +84,5 @@ interface DIDServiceInterface {
* @return JWK object
* @throws IllegalArgumentException if the DID format is invalid or conversion fails
*/
fun convertDIDToJWK(did:String):JWK
fun convertDIDToJWK(did: String, algorithm: JWSAlgorithm):JWK
}

0 comments on commit 5c94971

Please sign in to comment.