I am working on Aadhaar Paperless Offline e-kyc, trying to validate aadhaar XML signature using public key certificate. But I'm not sure is this the right way to do. Below is the java code for the reference.
public static void validateXMLSignature() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
ClassLoader classLoader = AadhaarXMLSignatureValidation.class.getClassLoader();
File file1 = new File("path-to-xml-file/aadhaar.xml");
Document document = db.parse(file1);
document.normalizeDocument();
// Find Signature element
NodeList nl =
document.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
if (nl.getLength() == 0) {
throw new Exception("Cannot find Signature element");
}
FileInputStream fin = new FileInputStream("path-to-certificate-file/certificate.cer");
CertificateFactory f = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate)f.generateCertificate(fin);
PublicKey publicKey = certificate.getPublicKey();
// Create a DOM XMLSignatureFactory that will be used to unmarshal the
// document containing the XMLSignature
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
// Create a DOMValidateContext and specify a X509KeySelector
// and document context
DOMValidateContext valContext = new DOMValidateContext(publicKey,
nl.item(0));
// unmarshal the XMLSignature
XMLSignature signature = fac.unmarshalXMLSignature(valContext);
// Validate the XMLSignature (generated above)
boolean coreValidity = signature.validate(valContext);
// Check core validation status
if (!coreValidity) {
System.err.println("Signature failed core validation");
} else {
System.out.println("Signature passed core validation");
}
}
can anyone tell me what I'm missing? Here is the link to Aadhaar Paperless Offline e-kyc tutorial https://uidai.gov.in/ecosystem/authentication-devices-documents/about-aadhaar-paperless-offline-e-kyc.html
You can try using below method to extract X509Certificate from the certificate string.