I was looking at some code of MyPGP to check whether an OpenPGP key is valid for encryption or not. RFC 4880 helped me understanding the code a bit. But given that I do not have a very good understanding of signature types, I am not able to clearly understand the following code piece:
private static boolean hasKeyFlags(PGPPublicKey key, int keyUsage) {
if (key.isMasterKey()) {
for (int certificationType : MASTER_KEY_CERTIFICATION_TYPES) {
Iterator eIt = key.getSignaturesOfType(certificationType);
while (eIt.hasNext()) {
PGPSignature signature = (PGPSignature) eIt.next();
if (!isMatchingUsage(signature, keyUsage))
return false;
}
}
} else {
Iterator eIt = key.getSignaturesOfType(PGPSignature.SUBKEY_BINDING);
while (eIt.hasNext()) {
PGPSignature signature = (PGPSignature) eIt.next();
if (!isMatchingUsage(signature, keyUsage))
return false;
}
}
return true;
}
where
private static final int[] MASTER_KEY_CERTIFICATION_TYPES = new int[]{
PGPSignature.POSITIVE_CERTIFICATION,
PGPSignature.CASUAL_CERTIFICATION,
PGPSignature.NO_CERTIFICATION,
PGPSignature.DEFAULT_CERTIFICATION
};
I not sure why are we looking for particular signature types for master keys and why looking into SUBKEY_BINDING
s otherwise.