1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.ws.security.transform;
21
22 import java.security.cert.CertificateEncodingException;
23 import java.security.cert.X509Certificate;
24
25 import org.apache.ws.security.WSConstants;
26 import org.apache.ws.security.WSDocInfo;
27 import org.apache.ws.security.WSSecurityException;
28 import org.apache.ws.security.message.token.SecurityTokenReference;
29 import org.apache.ws.security.message.token.X509Security;
30 import org.apache.ws.security.util.Base64;
31 import org.apache.ws.security.util.WSSecurityUtil;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34 import org.w3c.dom.Text;
35
36
37
38
39 public final class STRTransformUtil {
40 private static org.apache.commons.logging.Log log =
41 org.apache.commons.logging.LogFactory.getLog(STRTransformUtil.class);
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public static Element dereferenceSTR(Document doc,
58 SecurityTokenReference secRef, WSDocInfo wsDocInfo) throws WSSecurityException
59 {
60
61
62
63
64
65 if (secRef.containsReference()) {
66 if (log.isDebugEnabled()) {
67 log.debug("STR: Reference");
68 }
69 return secRef.getTokenElement(doc, wsDocInfo, null);
70 }
71
72
73
74
75 else if (secRef.containsX509Data() || secRef.containsX509IssuerSerial()) {
76 if (log.isDebugEnabled()) {
77 log.debug("STR: IssuerSerial");
78 }
79 X509Certificate[] certs =
80 secRef.getX509IssuerSerial(wsDocInfo.getCrypto());
81 if (certs == null || certs.length == 0 || certs[0] == null) {
82 throw new WSSecurityException(WSSecurityException.FAILED_CHECK);
83 }
84 return createBSTX509(doc, certs[0], secRef.getElement());
85 }
86
87
88
89
90
91 else if (secRef.containsKeyIdentifier()) {
92 if (log.isDebugEnabled()) {
93 log.debug("STR: KeyIdentifier");
94 }
95 if (WSConstants.WSS_SAML_KI_VALUE_TYPE.equals(secRef.getKeyIdentifierValueType())
96 || WSConstants.WSS_SAML2_KI_VALUE_TYPE.equals(secRef.getKeyIdentifierValueType())) {
97 return secRef.getTokenElement(doc, wsDocInfo, null);
98 } else {
99 X509Certificate[] certs = secRef.getKeyIdentifier(wsDocInfo.getCrypto());
100 if (certs == null || certs.length == 0 || certs[0] == null) {
101 throw new WSSecurityException(WSSecurityException.FAILED_CHECK);
102 }
103 return createBSTX509(doc, certs[0], secRef.getElement());
104 }
105 }
106 return null;
107 }
108
109 public static Element createBSTX509(Document doc, X509Certificate cert, Element secRefE)
110 throws WSSecurityException {
111 byte data[];
112 try {
113 data = cert.getEncoded();
114 } catch (CertificateEncodingException e) {
115 throw new WSSecurityException(
116 WSSecurityException.SECURITY_TOKEN_UNAVAILABLE, "encodeError", null, e
117 );
118 }
119 String prefix = WSSecurityUtil.getPrefixNS(WSConstants.WSSE_NS, secRefE);
120 if (prefix == null) {
121 prefix = WSConstants.WSSE_PREFIX;
122 }
123 Element elem = doc.createElementNS(WSConstants.WSSE_NS, prefix + ":BinarySecurityToken");
124 WSSecurityUtil.setNamespace(elem, WSConstants.WSSE_NS, prefix);
125
126 elem.setAttributeNS(null, "ValueType", X509Security.X509_V3_TYPE);
127 Text certText = doc.createTextNode(Base64.encode(data));
128 elem.appendChild(certText);
129 return elem;
130 }
131
132
133
134
135 private STRTransformUtil() {
136 }
137
138 }