Skip to content
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

Fix null pointer in VC verify #94

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
tomdaffurn marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,9 @@ public class VerifiableCredential internal constructor(public val vcDataModel: V
// using a set for fast string comparison. DIDs can be lonnng.
val verificationMethodIds = setOf(parsedDidUrl.didUrlString, "#${parsedDidUrl.fragment}")
val assertionMethods = didResolutionResult.didDocument.assertionMethodVerificationMethodsDereferenced
mistermoe marked this conversation as resolved.
Show resolved Hide resolved
var assertionMethod: VerificationMethod? = null

for (method in assertionMethods) {
val id = method.id.toString()
if (verificationMethodIds.contains(id)) {
assertionMethod = method
break
}
val assertionMethod = assertionMethods?.firstOrNull {
val id = it.id.toString()
verificationMethodIds.contains(id)
}

if (assertionMethod == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import web5.sdk.crypto.AwsKeyManager
import web5.sdk.crypto.InMemoryKeyManager
import web5.sdk.dids.CreateDidIonOptions
import web5.sdk.dids.DidIonHandle
import web5.sdk.dids.DidIonManager
import web5.sdk.dids.DidKey
import web5.sdk.dids.ion.model.PublicKey
import java.security.SignatureException
import java.text.ParseException
import java.util.UUID
import kotlin.test.Ignore
import kotlin.test.assertContains
import kotlin.test.assertEquals
Expand Down Expand Up @@ -143,6 +148,37 @@ class VerifiableCredentialTest {
VerifiableCredential.verify(vcJwt)
}

@Test
fun `verify handles DIDs without an assertionMethod`() {
val keyManager = InMemoryKeyManager()

//Create an ION DID without an assertionMethod
val alias = keyManager.generatePrivateKey(JWSAlgorithm.ES256K)
val verificationJwk = keyManager.getPublicKey(alias)
val key = PublicKey(
id = UUID.randomUUID().toString(),
type = "JsonWebKey2020",
publicKeyJwk = verificationJwk,
purposes = emptyList() //No assertionMethod
)
val issuerDid = DidIonManager.create(
InMemoryKeyManager(),
CreateDidIonOptions(verificationPublicKey = key)
)

val header = JWSHeader.Builder(JWSAlgorithm.ES256K)
.keyID(issuerDid.uri)
.build()
//A detached payload JWT
val vcJwt = "${header.toBase64URL()}..fakeSig"

val exception = assertThrows(SignatureException::class.java) {
VerifiableCredential.verify(vcJwt)
}
assertEquals("Signature verification failed: Expected kid in JWS header to dereference a DID Document " +
"Verification Method with an Assertion verification relationship", exception.message)
}

@Test
fun `parseJwt throws ParseException if argument is not a valid JWT`() {
assertThrows(ParseException::class.java) {
Expand Down
Loading