Статус: Участник
Группы: Участники
Зарегистрирован: 27.02.2013(UTC) Сообщений: 12 Сказал(а) «Спасибо»: 2 раз
|
Спасибо, помогло. Выкладываю финальную версию рабочего алгоритма: Код:
System.out.println("Signed algoritm - "+alg);
com.sun.org.apache.xml.internal.security.Init.init();
SpecUtility.initJCP();
KeyStore keyStore = KeyStore.getInstance(JCP.HD_STORE_NAME);
keyStore.load(null, null);
System.out.println("Sign cert name - " + SpecUtility.DEFAULT_ALIAS);
PrivateKey privateKey = (PrivateKey)keyStore.getKey(SpecUtility.DEFAULT_ALIAS, SpecUtility.DEFAULT_PASSWORD);
X509Certificate cert = (X509Certificate) keyStore.getCertificate(SpecUtility.DEFAULT_ALIAS);
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setValidating(false);
DocumentBuilder builder = f.newDocumentBuilder();
Document doc = builder.parse(new File(inSOAPFile));
Element token = doc.getDocumentElement();
token = (Element)doc.getElementsByTagName("mkgu:body").item(0);
System.out.println(org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc));
Provider xmlDSigProvider = new ru.CryptoPro.JCPxml.dsig.internal.dom.XMLDSigRI();
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM", xmlDSigProvider);
List<Transform> transformList = new ArrayList<Transform>();
Transform transform = fac.newTransform(Transform.ENVELOPED, (XMLStructure) null);
Transform transformC14N = fac.newTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS, (XMLStructure) null);
transformList.add(transform);
transformList.add(transformC14N);
Reference ref = fac.newReference("#mkgu", fac.newDigestMethod("http://www.w3.org/2001/04/xmldsig-more#gostr3411", null),
transformList, null, null);
SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE,
(C14NMethodParameterSpec) null),
fac.newSignatureMethod("http://www.w3.org/2001/04/xmldsig-more#gostr34102001-gostr3411", null),
Collections.singletonList(ref));
KeyInfoFactory kif = fac.getKeyInfoFactory();
X509Data x509d = kif.newX509Data(Collections.singletonList(cert));
KeyInfo ki = kif.newKeyInfo(Collections.singletonList(x509d));
javax.xml.crypto.dsig.XMLSignature sig = fac.newXMLSignature(si, ki);
DOMSignContext signContext = new DOMSignContext(privateKey, token);
signContext.putNamespacePrefix(XMLSignature.XMLNS, "ds");
signContext.setIdAttributeNS(token, null, "id");
signContext.putNamespacePrefix(javax.xml.crypto.dsig.XMLSignature.XMLNS, "ds");
sig.sign(signContext);
System.out.println(org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc));
System.out.println("----");
Element sigE = (Element) token.getElementsByTagName("ds:Signature").item(0);
Node keyE = XPathAPI.selectSingleNode(sigE, "//ds:KeyInfo", sigE);
String xpat = XPathAPI.selectSingleNode(keyE, "//ds:X509Certificate", keyE).getFirstChild().getNodeValue();
Text diget = doc.createTextNode(xpat);
keyE.removeChild(XPathAPI.selectSingleNode(keyE, "//ds:X509Data", keyE));
NodeList chl = keyE.getChildNodes();
for (int i = 0; i < chl.getLength(); i++) {
keyE.removeChild(chl.item(i));
}
Node str = keyE.appendChild(doc.createElementNS("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "wsse:SecurityTokenReference"));
Element strRef = (Element) str.appendChild(doc.createElementNS("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "wsse:Reference"));
strRef.setAttribute("ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
strRef.setAttribute("URI", "#CertId");
doc.getDocumentElement().appendChild(sigE);
String mes = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
System.out.println(mes);
Array.writeFile(inSOAPFile + ".signed.text.xml", mes.getBytes("UTF-8"));
|