-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestClass.java
198 lines (165 loc) · 8.73 KB
/
TestClass.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.att.eg.catalog.cms.ingest.gitignore;
import org.w3c.dom.Element;
import javax.xml.crypto.dsig.*;
import javax.xml.crypto.dsig.dom.DOMSignContext;
import javax.xml.crypto.dsig.keyinfo.KeyInfo;
import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory;
import javax.xml.crypto.dsig.keyinfo.KeyValue;
import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;
import javax.xml.soap.*;
import javax.xml.transform.stream.StreamSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.StringReader;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.*;
import java.util.*;
import static com.sun.org.apache.xml.internal.security.signature.XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1;
public class TestClass {
private static String ENVELOPE = "<soapenv:Envelope " +
"xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
"xmlns:SOAP-SEC=\"http://schemas.xmlsoap.org/soap/security/2000-12\"\n" +
" xmlns:ns=\"http://example.com/ns\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body id=\"Demo\">\n" +
" <ns:request>\n" +
" <ns:customer>\n" +
" <ns:id>123</ns:id>\n" +
" <ns:name type=\"NCHZ\">John Brown</ns:name>\n" +
" </ns:customer>\n" +
" </ns:request>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>";
public static SOAPMessage readSoapMessage() throws SOAPException,
FileNotFoundException {
SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPPart soapPart = message.getSOAPPart();
soapPart.setContent(new StreamSource(new StringReader(ENVELOPE)));
message.saveChanges();
return message;
}
private static Element getFirstChildElement(Node node) {
Node child = (Node) node.getFirstChild();
while ((child != null) && (child.getNodeType() != Node.ELEMENT_NODE)) {
child = (Node) child.getNextSibling();
}
return (Element) child;
}
public static Element getNextSiblingElement(Node node) {
Node sibling = (Node) node.getNextSibling();
while ((sibling != null) && (sibling.getNodeType() != Node.ELEMENT_NODE)) {
sibling = (Node) sibling.getNextSibling();
}
return (Element) sibling;
}
public static void main(String[] args) throws Exception {
KeyStore p12 = KeyStore.getInstance("pkcs12");
char[] password = "ndlsonline".toCharArray();
p12.load(new FileInputStream("/home/svarkey/D20160103/BUF/DLWSClientStore.p12"), password);
// p12.load(new FileInputStream("//home/svarkey/D20160103/BUF/DLWSClientSSLStore.pfx"), password);
X509Certificate cert = (X509Certificate) p12.getCertificate("wsstestservice");
System.out.println(cert.getSerialNumber());
System.out.println(new String(Base64.getEncoder().encode(cert.getEncoded())));
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println();
/*
//System.exit(0);
List<X509Certificate> certs = new ArrayList<>();
Enumeration e = p12.aliases();
while (e.hasMoreElements()) {
String alias = (String) e.nextElement();
X509Certificate c = (X509Certificate) p12.getCertificate(alias);
certs.add(c);
}*/
java.security.cert.Certificate[] cchain = p12.getCertificateChain("wsstest");
List<Certificate> mylist = new ArrayList();
for (int i = 0; i < cchain.length; i++) {
mylist.add(cchain[i]);
}
CertificateFactory cf = CertificateFactory.getInstance("X.509");
CertPath cp = cf.generateCertPath(mylist);
//System.out.println(cp.getEncoded());
System.out.println(new String(Base64.getEncoder().encode(cp.getEncoded())));
//System.out.println(new String(Base64.getEncoder().encode(mylist.get(0).getEncoded())));
System.exit(0);
/*
Set trust = new HashSet();
trust.add(new TrustAnchor(cert, null));
CollectionCertStoreParameters certStoreParams = new CollectionCertStoreParameters(certs);
CertStore certStore = CertStore.getInstance("Collection", certStoreParams);
// specify the target certificate via a CertSelector
X509CertSelector certSelector = new X509CertSelector();
X509Certificate userCert = (X509Certificate)p12.getCertificate("wsstest");
certSelector.setCertificate(userCert);
certSelector.setSubject(userCert.getSubjectDN().getName()); // seems to be required
// build a valid cerificate path
CertPathBuilder certPathBuilder = CertPathBuilder.getInstance("PKIX", "SUN");
PKIXBuilderParameters certPathBuilderParams = new PKIXBuilderParameters(trust, certSelector);
certPathBuilderParams.addCertStore(certStore);
certPathBuilderParams.setRevocationEnabled(false);
CertPathBuilderResult result = certPathBuilder.build(certPathBuilderParams);
CertPath path = result.getCertPath();
*/
//System.out.println(new String(Base64.getEncoder().encode(path.getEncoded())));
//CertPathBuilderResult a = cpb.build(cpp);
//CertPath certPath = a.getCertPath();
/* CollectionCertStoreParameters certStoreParams = new CollectionCertStoreParameters(certs);
CertStore certStore = CertStore.getInstance("Collection", certStoreParams);
// specify the target certificate via a CertSelector
X509CertSelector certSelector = new X509CertSelector();
certSelector.setCertificate(userCert);
certSelector.setSubject(userCert.getSubjectDN().getName()); // seems to be required
// build a valid cerificate path
CertPathBuilder certPathBuilder = CertPathBuilder.getInstance("PKIX", "SUN");
PKIXBuilderParameters certPathBuilderParams = new PKIXBuilderParameters(trustAnchors, certSelector);
certPathBuilderParams.addCertStore(certStore);
certPathBuilderParams.setRevocationEnabled(false);
CertPathBuilderResult result = certPathBuilder.build(certPathBuilderParams);*/
System.exit(0);
Enumeration e = p12.aliases();
while (e.hasMoreElements()) {
String alias = (String) e.nextElement();
X509Certificate c = (X509Certificate) p12.getCertificate(alias);
PrivateKey privateKey = (PrivateKey) p12.getKey(alias, password);
System.out.println(alias + " alias " + (privateKey != null ? privateKey.getFormat() : null));
X509Certificate cert1 = (X509Certificate) p12.getCertificate("wsstestservice");
System.out.println(new String(Base64.getEncoder().encode(cert.getEncoded())));
if (true) {
continue;
}
KeyPair keyPair = new KeyPair(cert.getPublicKey(), privateKey);
XMLSignatureFactory sigFactory = XMLSignatureFactory.getInstance();
Reference ref = sigFactory.newReference("#Demo", sigFactory.newDigestMethod(DigestMethod.SHA1, null));
SignedInfo signedInfo = sigFactory.newSignedInfo(sigFactory.newCanonicalizationMethod(
CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, (C14NMethodParameterSpec) null), sigFactory
.newSignatureMethod(SignatureMethod.DSA_SHA1, null), Collections.singletonList(ref));
KeyInfoFactory kif = sigFactory.getKeyInfoFactory();
KeyValue kv = kif.newKeyValue(keyPair.getPublic());
KeyInfo keyInfo = kif.newKeyInfo(Collections.singletonList(kv));
XMLSignature xmlSig = sigFactory.newXMLSignature(signedInfo, keyInfo);
SOAPMessage envelope = readSoapMessage();
SOAPEnvelope soapEnvelope = envelope.getSOAPPart().getEnvelope();
System.out.println(soapEnvelope.getBody());
System.out.println(soapEnvelope.getHeader());
DOMSignContext sigContext = new DOMSignContext(privateKey, soapEnvelope.getBody());
sigContext.putNamespacePrefix(XMLSignature.XMLNS, "SOAP-SEC");
//sigContext.setIdAttributeNS(getNextSiblingElement(header),
//a "http://schemas.xmlsoap.org/soap/security/2000-12", "id");
xmlSig.sign(sigContext);
/* Principal subject = c.getSubjectDN();
String subjectArray[] = subject.toString().split(",");
for (String s : subjectArray) {
String[] str = s.trim().split("=");
String key = str[0];
String value = str[1];
//System.out.println(key + " - " + value);
}*/
}
}
}