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

build-logic: Support m out of n signature verification #2460

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,27 @@ class SignatureVerifier(
): Boolean {
Security.addProvider(BouncyCastleProvider())

var isSuccess = true
val signatureFileInBytes = readSignatureFromFile(signatureFile)
val pgpObjectFactory = JcaPGPObjectFactory(signatureFileInBytes)
val signatureList: PGPSignatureList = pgpObjectFactory.nextObject() as PGPSignatureList

val signatureVerificationResult = mutableListOf<Boolean>()
pgpFingerprintToKeyUrl.forEach { (fingerprint, keyUrl) ->
val ppgPublicKeyParser = PpgPublicKeyParser(fingerprint, keyUrl)
ppgPublicKeyParser.parse()

val isSignedByAnyKey = verifyDetachedSignature(
potentialSigningKeys = ppgPublicKeyParser.keyById,
pgpSignatureByteArray = readSignatureFromFile(signatureFile),
signatureList = signatureList,
data = fileToVerify.readBytes()
)

isSuccess = isSuccess && isSignedByAnyKey
signatureVerificationResult.add(isSignedByAnyKey)
}

return isSuccess
val numberOfSuccessfulVerifications = signatureVerificationResult.filter { isSuccess -> isSuccess }
.count()
return numberOfSuccessfulVerifications == signatureList.size()
}

private fun readSignatureFromFile(signatureFile: File): ByteArray {
Expand All @@ -48,12 +54,9 @@ class SignatureVerifier(

private fun verifyDetachedSignature(
potentialSigningKeys: Map<Long, PGPPublicKey>,
pgpSignatureByteArray: ByteArray,
signatureList: PGPSignatureList,
data: ByteArray
): Boolean {
val pgpObjectFactory = JcaPGPObjectFactory(pgpSignatureByteArray)
val signatureList: PGPSignatureList = pgpObjectFactory.nextObject() as PGPSignatureList

var pgpSignature: PGPSignature? = null
var signingKey: PGPPublicKey? = null

Expand All @@ -65,7 +68,10 @@ class SignatureVerifier(
}
}

checkNotNull(signingKey) { "signingKey not found" }
if (signingKey == null) {
return false
}

checkNotNull(pgpSignature) { "signature for key not found" }

pgpSignature.init(
Expand Down
Loading