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

feat: Added MultiSignaturePolicy #350

Merged
merged 1 commit into from
Sep 22, 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
1 change: 1 addition & 0 deletions src/main/kotlin/id/walt/auditor/PolicyRegistryService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ open class PolicyRegistryService : WaltIdService() {
)
register(CredentialStatusPolicy::class, "Verify by credential status")
register(DynamicPolicy::class, DynamicPolicyArg::class, "Verify credential by rego policy")
register(MultiSignaturePolicy::class, "Verify embedded multiple signatures")

// predefined, hardcoded rego policy specializations
// VerifiableMandate policy as specialized rego policy
Expand Down
37 changes: 37 additions & 0 deletions src/main/kotlin/id/walt/auditor/policies/MultiSignaturePolicy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package id.walt.auditor.policies

import id.walt.auditor.SimpleVerificationPolicy
import id.walt.auditor.VerificationPolicyResult
import id.walt.credentials.w3c.VerifiableCredential

class JwtHelper(val credential: String) {
val header get() = credential.substringBefore(".")
val payload get() = credential.substringAfter(".").substringBefore(".")
val signature get() = credential.substringAfterLast(".")
val jwsSignaturePart get() = mapOf(
"protected" to header,
"signature" to signature
)

companion object {
fun fromJWS(payload: String, sig: Map<String, String>): JwtHelper {
val h = sig["protected"] ?: throw Exception("No header found")
val s = sig["signature"] ?: throw Exception("No sig found")
return JwtHelper("$h.$payload.$s")
}
}
}

class MultiSignaturePolicy: SimpleVerificationPolicy() {
override val description: String
get() = "JWS Multi Signature Verification Policy"

override fun doVerify(vc: VerifiableCredential): VerificationPolicyResult {
val payload = (vc.credentialSubject?.properties?.get("payload") as? String) ?: return VerificationPolicyResult.failure()
val signatures = (vc.credentialSubject?.properties?.get("signatures") as? List<Map<String, String>>) ?: return VerificationPolicyResult.failure()
val credentials = signatures.map { JwtHelper.fromJWS(payload, it).credential }
return if(credentials.all { SignaturePolicy().verify(VerifiableCredential.fromString(it)).isSuccess }) {
VerificationPolicyResult.success()
} else VerificationPolicyResult.failure()
}
}