如何借助Java中的公钥证书验证aadhar XML签名?
我正在研究 Aadhaar 无纸化离线 e-kyc,尝试使用公钥证书验证 aadhaar XML 签名。但我不确定这是否是正确的做法。下面是java代码供参考。
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");
}
}
谁能告诉我我错过了什么? 以下是 Aadhaar 无纸化离线 e-kyc 教程的链接 https://uidai.gov.in/ecosystem/authentication-devices-documents/about-aadhaar-paperless-offline-e-kyc.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用以下方法从证书字符串中提取 X509Certificate。
You can try using below method to extract X509Certificate from the certificate string.
我在验证 aadhaar XML 签名时也遇到了一些问题。只是在这里分享工作代码片段。
重要的是在公钥证书中附加 BEGIN 和 END 格式,并且它已经是 base64 字符串,因此无需对其进行解码。
i also faced some issues while validating aadhaar XML signature. just sharing working code snippet here.
important is to append BEGIN and END format in public key certificate and it's already base64 string so no need to decode the same.