}
private boolean validate(PublicKey publickey, JonasIdentity identity) throws Exception {
// Build signature with data to validate (principal name + roles)
Signature signature = null;
try {
signature = Signature.getInstance("SHA1withDSA");
} catch (NoSuchAlgorithmException e) {
if (logger.isLoggable(BasicLevel.ERROR))
logger.log(BasicLevel.ERROR, "EXCEPTION:: validate", e);
throw new Exception("Error while getting the algorithm 'SHA1withDSA' :" + e.getMessage());
}
try {
signature.initVerify(publickey);
} catch (InvalidKeyException e) {
if (logger.isLoggable(BasicLevel.ERROR))
logger.log(BasicLevel.ERROR, "EXCEPTION:: validate", e);
throw new Exception("Cannot initialize the signature with the given public key:" + e.getMessage());
}
// Add principal name
String principal = null;
try {
//signature.update(principal.getBytes());
principal = identity.getPrincipal();
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, "validate principal = " + principal);
if (principal == null) {
if (logger.isLoggable(BasicLevel.ERROR))
logger.log(BasicLevel.ERROR, "EXCEPTION:: validate principal == null");
throw new Exception("Cannot add the bytes for the principal name '" + principal + "'");
}
signature.update(principal.getBytes());
} catch (SignatureException e) {
if (logger.isLoggable(BasicLevel.ERROR))
logger.log(BasicLevel.ERROR, "EXCEPTION:: validate", e);
throw new Exception("Cannot add the bytes for the principal name '" + principal + "' :" + e.getMessage());
}
// Add roles
Object[] roles = identity.getRoles();
if (!Configuration.getBoolean(UNSORT_ROLES)) {
// Sort roles before adding it to the signature to preserve the order
Arrays.sort(roles);
}
for (int r = 0; r < roles.length; r++) {
try {
signature.update(((String)roles[r]).getBytes());
} catch (SignatureException e) {
if (logger.isLoggable(BasicLevel.ERROR))
logger.log(BasicLevel.ERROR, "EXCEPTION:: validate", e);
throw new Exception("Cannot add the bytes for the role '" + roles[r] + "' : " + e.getMessage());
}
}
// Check signature
boolean trusted = false;
try {
trusted = signature.verify(identity.getSignature());
} catch (SignatureException e) {
if (logger.isLoggable(BasicLevel.ERROR))
logger.log(BasicLevel.ERROR, "EXCEPTION:: validate", e);
throw new Exception("The signature found in the identity '" + this + "' is invalid:" + e.getMessage());
}