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.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   * Utility class exposing the dereferencing logic of the {@link STRTransform} implementation.
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       * Retrieves the element representing the referenced content of a STR.
45       * 
46       * @return the element representing the referenced content. The element is either
47       *         extracted from {@code doc} or a new element is created in the
48       *         case of a key identifier or issuer serial STR.  {@code null} if
49       *         {@code secRef} does not contain a direct reference, key identifier, or
50       *         issuer serial.
51       * @throws WSSecurityException
52       *             If an issuer serial or key identifier is used in the STR and
53       *             the certificate cannot be resolved from the crypto
54       *             configuration or if there is an error working with the resolved
55       *             cert
56       */
57      public static Element dereferenceSTR(Document doc,
58              SecurityTokenReference secRef, WSDocInfo wsDocInfo) throws WSSecurityException
59      {
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              if (log.isDebugEnabled()) {
67                  log.debug("STR: Reference");
68              }
69              return secRef.getTokenElement(doc, wsDocInfo, null);
70          }
71          //
72          // second case: IssuerSerial, lookup in keystore, wrap in BST according
73          // to specification
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          // third case: KeyIdentifier. For SKI, lookup in keystore, wrap in
88          // BST according to specification. Otherwise if it's a wsse:KeyIdentifier it could
89          // be a SAML assertion, so try and find the referenced element.
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         // elem.setAttributeNS(WSConstants.XMLNS_NS, "xmlns", "");
126         elem.setAttributeNS(null, "ValueType", X509Security.X509_V3_TYPE);
127         Text certText = doc.createTextNode(Base64.encode(data)); // no line wrap
128         elem.appendChild(certText);
129         return elem;
130     }
131     
132     /**
133      * Hidden in utility class.
134      */
135     private STRTransformUtil() {   
136     }
137     
138 }