View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.wss4j.dom.transform;
21  
22  import java.security.cert.CertificateEncodingException;
23  import java.security.cert.X509Certificate;
24  
25  import org.apache.wss4j.common.ext.WSSecurityException;
26  import org.apache.wss4j.common.token.Reference;
27  import org.apache.wss4j.common.token.SecurityTokenReference;
28  import org.apache.wss4j.common.token.X509Security;
29  import org.apache.wss4j.common.util.XMLUtils;
30  import org.apache.wss4j.dom.WSConstants;
31  import org.apache.wss4j.dom.WSDocInfo;
32  import org.apache.wss4j.dom.str.STRParserUtil;
33  import org.w3c.dom.Document;
34  import org.w3c.dom.Element;
35  import org.w3c.dom.Text;
36  
37  /**
38   * Utility class exposing the dereferencing LOG.c of the {@link STRTransform} implementation.
39   */
40  public final class STRTransformUtil {
41      private static final org.slf4j.Logger LOG =
42          org.slf4j.LoggerFactory.getLogger(STRTransformUtil.class);
43  
44      /**
45       * Retrieves the element representing the referenced content of a STR.
46       *
47       * @return the element representing the referenced content. The element is either
48       *         extracted from {@code doc} or a new element is created in the
49       *         case of a key identifier or issuer serial STR.  {@code null} if
50       *         {@code secRef} does not contain a direct reference, key identifier, or
51       *         issuer serial.
52       * @throws WSSecurityException
53       *             If an issuer serial or key identifier is used in the STR and
54       *             the certificate cannot be resolved from the crypto
55       *             configuration or if there is an error working with the resolved
56       *             cert
57       */
58      public static Element dereferenceSTR(Document doc,
59              SecurityTokenReference secRef, WSDocInfo wsDocInfo) throws WSSecurityException {
60          //
61          // First case: direct reference, according to chap 7.2 of OASIS WS
62          // specification (main document). Only in this case return a true
63          // reference to the BST or Assertion. Copying is done by the caller.
64          //
65          if (secRef.containsReference()) {
66              LOG.debug("STR: Reference");
67  
68              Reference reference = secRef.getReference();
69              return STRParserUtil.getTokenElement(doc, wsDocInfo, null, reference.getURI(), reference.getValueType());
70          } else if (secRef.containsX509Data() || secRef.containsX509IssuerSerial()) {
71              //
72              // second case: IssuerSerial, lookup in keystore, wrap in BST according
73              // to specification
74              //
75              LOG.debug("STR: IssuerSerial");
76              X509Certificate[] certs =
77                  secRef.getX509IssuerSerial(wsDocInfo.getCrypto());
78              if (certs == null || certs.length == 0 || certs[0] == null) {
79                  throw new WSSecurityException(WSSecurityException.ErrorCode.FAILED_CHECK);
80              }
81              return createBSTX509(doc, certs[0], secRef.getElement(), secRef.getKeyIdentifierEncodingType());
82          } else if (secRef.containsKeyIdentifier()) {
83              //
84              // third case: KeyIdentifier. For SKI, lookup in keystore, wrap in
85              // BST according to specification. Otherwise if it's a wsse:KeyIdentifier it could
86              // be a SAML assertion, so try and find the referenced element.
87              //
88              LOG.debug("STR: KeyIdentifier");
89              if (WSConstants.WSS_SAML_KI_VALUE_TYPE.equals(secRef.getKeyIdentifierValueType())
90                  || WSConstants.WSS_SAML2_KI_VALUE_TYPE.equals(secRef.getKeyIdentifierValueType())) {
91                  return STRParserUtil.getTokenElement(doc, wsDocInfo, null, secRef.getKeyIdentifierValue(),
92                                                       secRef.getKeyIdentifierValueType());
93              } else {
94                  X509Certificate[] certs = secRef.getKeyIdentifier(wsDocInfo.getCrypto());
95                  if (certs == null || certs.length == 0 || certs[0] == null) {
96                      throw new WSSecurityException(WSSecurityException.ErrorCode.FAILED_CHECK);
97                  }
98                  return createBSTX509(doc, certs[0], secRef.getElement());
99              }
100         }
101         return null;
102     }
103 
104     public static Element createBSTX509(Document doc, X509Certificate cert, Element secRefE)
105         throws WSSecurityException {
106         return createBSTX509(doc, cert, secRefE, null);
107     }
108 
109     public static Element createBSTX509(Document doc, X509Certificate cert, Element secRefE,
110                                         String secRefEncType)
111         throws WSSecurityException {
112         byte[] data;
113         try {
114             data = cert.getEncoded();
115         } catch (CertificateEncodingException e) {
116             throw new WSSecurityException(
117                 WSSecurityException.ErrorCode.SECURITY_TOKEN_UNAVAILABLE, e, "encodeError"
118             );
119         }
120         String prefix = XMLUtils.getPrefixNS(WSConstants.WSSE_NS, secRefE);
121         if (prefix == null) {
122             prefix = WSConstants.WSSE_PREFIX;
123         }
124         Element elem = doc.createElementNS(WSConstants.WSSE_NS, prefix + ":BinarySecurityToken");
125         XMLUtils.setNamespace(elem, WSConstants.WSSE_NS, prefix);
126         // elem.setAttributeNS(WSConstants.XMLNS_NS, "xmlns", "");
127         elem.setAttributeNS(null, "ValueType", X509Security.X509_V3_TYPE);
128         if (secRefEncType != null) {
129             elem.setAttributeNS(null, "EncodingType", secRefEncType);
130         }
131         Text certText = doc.createTextNode(org.apache.xml.security.utils.XMLUtils.encodeToString(data)); // no line wrap
132         elem.appendChild(certText);
133         return elem;
134     }
135 
136     /**
137      * Hidden in utility class.
138      */
139     private STRTransformUtil() {
140     }
141 
142 }