| 12
 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
 
 | /*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ws.extensions.security;
import java.lang.reflect.Constructor;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import org.jboss.ws.extensions.security.element.EncryptedKey;
import org.jboss.ws.extensions.security.element.SecurityHeader;
import org.jboss.ws.extensions.security.element.SecurityProcess;
import org.jboss.ws.extensions.security.element.Signature;
import org.jboss.ws.extensions.security.element.Timestamp;
import org.jboss.ws.extensions.security.element.Token;
import org.jboss.ws.extensions.security.element.UsernameToken;
import org.jboss.ws.extensions.security.exception.WSSecurityException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
 * @author <a href="mailto:jason.greene@jboss.com">Jason T. Greene</a>
 * @version $Revision: 5034 $
 */
public class SecurityDecoder
{
   private Element headerElement;
   private Calendar now =  null;
   private SecurityHeader header;
   private Document message;
   private SecurityStore store;
   private HashSet<String> signedIds = new HashSet<String>();
   private HashSet<String> encryptedIds = new HashSet<String>();
   public SecurityDecoder(SecurityStore store)
   {
      org.apache.xml.security.Init.init();
      this.store = store;
   }
   /**
    * A special constructor that allows you to use a different value when validating the message.
    * DO NOT USE THIS UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!.
    *
    * @param SecurityStore the security store that contains key and trust information
    * @param now The timestamp to use as the current time when validating a message expiration
    */
   public SecurityDecoder(SecurityStore store, Calendar now)
   {
      this(store);
      this.now = now;
   }
   private Element getHeader(Document message) throws WSSecurityException
   {
      Element header = Util.findElement(message.getDocumentElement(), "Security", Constants.WSSE_NS);
      if (header == null)
         throw new WSSecurityException("Expected security header was not found");
      return header;
   }
   private void detachHeader()
   {
      headerElement.getParentNode().removeChild(headerElement);
   }
   private void decode() throws WSSecurityException
   {
      // Validate a timestamp if it is present
      Timestamp timestamp = header.getTimestamp();
      if (timestamp != null)
      {
         TimestampVerificationOperation operation =
            (now == null) ? new TimestampVerificationOperation() : new TimestampVerificationOperation(now);
         operation.process(message, timestamp);
      }
      for (Token token : header.getTokens())
      {
         if (token instanceof UsernameToken)
            new ReceiveUsernameOperation(header, store).process(message, token);
      }
      signedIds.clear();
      encryptedIds.clear();
      SignatureVerificationOperation signatureVerifier = new SignatureVerificationOperation(header, store);
      DecryptionOperation decrypter = new DecryptionOperation(header, store);
      for (SecurityProcess process : header.getSecurityProcesses())
      {
         // If this list gets much larger it should probably be a hash lookup
         if (process instanceof Signature)
         {
            Collection<String> ids = signatureVerifier.process(message, process);
            if (ids != null)
              signedIds.addAll(ids);
         }
         else if (process instanceof EncryptedKey)
         {
            Collection<String> ids = decrypter.process(message, process);
            if (ids != null)
               encryptedIds.addAll(ids);
         }
      }
   }
   public void verify(List<OperationDescription<RequireOperation>> requireOperations) throws WSSecurityException
   {
      if (requireOperations == null)
         return;
      for (OperationDescription<RequireOperation> o : requireOperations)
      {
         Class<? extends RequireOperation> operation = o.getOperation();
         RequireOperation op;
         Collection<String> processedIds = null;
         if (operation.equals(RequireSignatureOperation.class))
         {
            op = new RequireSignatureOperation(header, store);
            processedIds = signedIds;
         }
         else if (operation.equals(RequireEncryptionOperation.class))
         {
            op = new RequireEncryptionOperation(header, store);
            processedIds = encryptedIds;
         }
         else
         {
            try
            {
               Constructor<? extends RequireOperation> c = operation.getConstructor(SecurityHeader.class, SecurityStore.class);
               op = c.newInstance(header, store);
            }
            catch (Exception e)
            {
               throw new WSSecurityException("Error constructing operation: " + operation);
            }
         }
         op.process(message, o.getTargets(), o.getCertificateAlias(), o.getCredential(), processedIds);
      }
   }
   public void decode(Document message) throws WSSecurityException
   {
      decode(message, getHeader(message));
   }
   public void decode(Document message, Element headerElement) throws WSSecurityException
   {
      this.headerElement = headerElement;
      this.header = new SecurityHeader(this.headerElement, store);
      this.message = message;
      decode();
   }
   public void complete()
   {
      // On completion we must remove the header so that no one else can process this
      // message (required by the specification)
      detachHeader();
   }
}
 |