001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.mts.std;
028:
029: import org.cougaar.bootstrap.SystemProperties;
030: import org.cougaar.core.mts.Message;
031: import org.cougaar.core.mts.MessageAddress;
032: import org.cougaar.core.mts.MessageAttributes;
033: import org.cougaar.core.mts.MessageSecurityManager;
034: import org.cougaar.core.node.DummyMessageSecurityManager;
035: import org.cougaar.core.node.SecureMessage;
036:
037: import org.cougaar.mts.base.MisdeliveredMessageException;
038: import org.cougaar.mts.base.CommFailureException;
039: import org.cougaar.mts.base.UnregisteredNameException;
040: import org.cougaar.mts.base.NameLookupException;
041: import org.cougaar.mts.base.DestinationLink;
042: import org.cougaar.mts.base.DestinationLinkDelegateImplBase;
043: import org.cougaar.mts.base.MessageDeliverer;
044: import org.cougaar.mts.base.MessageDelivererDelegateImplBase;
045: import org.cougaar.mts.base.StandardAspect;
046:
047: /**
048: * This Aspect uses the (obsolete?) {@link MessageSecurityManager}
049: * interface to secure message traffic in a simple way.
050: *
051: * @property org.cougaar.message.security specifies the implementation
052: * class of the {@link MessageSecurityManager}. If unspecified the
053: * dummy implementation {@link DummyMessageSecurityManager} is used.
054: */
055: public class SecurityAspect extends StandardAspect {
056: private static final String SECURITY_CLASS_PROPERTY = "org.cougaar.message.security";
057: private static MessageSecurityManager msm = null;
058:
059: private static synchronized MessageSecurityManager ensure_msm() {
060: if (msm != null)
061: return msm;
062:
063: String name = SystemProperties
064: .getProperty(SECURITY_CLASS_PROPERTY);
065: if (name != null && (!name.equals(""))
066: && (!name.equals("none"))) {
067: try {
068: // Object raw = Beans.instantiate(null, name);
069: Object raw = Class.forName(name).newInstance();
070: msm = (MessageSecurityManager) raw;
071: } catch (Exception ex) {
072: }
073: } else {
074: msm = new DummyMessageSecurityManager();
075: }
076: return msm;
077: }
078:
079: private boolean enabled = false;
080:
081: public SecurityAspect() {
082: enabled = ensure_msm() != null;
083: }
084:
085: public boolean isEnabled() {
086: return enabled;
087: }
088:
089: // Temporarily package access, rather than private, until we get
090: // rid of MessageTransportClassic
091: AttributedMessage secure(AttributedMessage message) {
092: if (msm != null) {
093: if (loggingService.isDebugEnabled())
094: loggingService.debug("Securing message " + message);
095: Message rawMessage = message.getRawMessage();
096: Message secureMsg = msm.secureMessage(rawMessage);
097: return new AttributedMessage(secureMsg, message);
098: } else {
099: return message;
100: }
101: }
102:
103: // Temporarily package access, rather than private, until we get
104: // rid of MessageTransportClassic
105: AttributedMessage unsecure(AttributedMessage message) {
106: if (msm == null) {
107: if (loggingService.isErrorEnabled())
108: loggingService.error("MessageTransport " + this
109: + " received SecureMessage " + message
110: + " but has no MessageSecurityManager.");
111: return null;
112: } else {
113: if (loggingService.isDebugEnabled())
114: loggingService.debug("Unsecuring message " + message);
115: SecureMessage rawMessage = (SecureMessage) message
116: .getRawMessage();
117: Message originalMessage = msm.unsecureMessage(rawMessage);
118: AttributedMessage msg = new AttributedMessage(
119: originalMessage, message);
120: if (msg == null && loggingService.isErrorEnabled()) {
121: loggingService.error("MessageTransport " + this
122: + " received an unverifiable SecureMessage "
123: + message);
124: }
125: return msg;
126: }
127: }
128:
129: public Object getDelegate(Object delegate, Class type) {
130: if (type == DestinationLink.class) {
131: DestinationLink link = (DestinationLink) delegate;
132: return new SecureDestinationLink(link);
133: } else {
134: return null;
135: }
136: }
137:
138: public Object getReverseDelegate(Object delegate, Class type) {
139: if (type == MessageDeliverer.class) {
140: return new SecureDeliverer((MessageDeliverer) delegate);
141: } else {
142: return null;
143: }
144: }
145:
146: private class SecureDestinationLink extends
147: DestinationLinkDelegateImplBase {
148: private SecureDestinationLink(DestinationLink link) {
149: super (link);
150: }
151:
152: public MessageAttributes forwardMessage(
153: AttributedMessage message)
154: throws UnregisteredNameException, NameLookupException,
155: CommFailureException, MisdeliveredMessageException {
156: return super .forwardMessage(secure(message));
157: }
158:
159: }
160:
161: private class SecureDeliverer extends
162: MessageDelivererDelegateImplBase {
163: private SecureDeliverer(MessageDeliverer deliverer) {
164: super (deliverer);
165: }
166:
167: public MessageAttributes deliverMessage(AttributedMessage m,
168: MessageAddress dest)
169: throws MisdeliveredMessageException {
170: return super.deliverMessage(unsecure(m), dest);
171: }
172:
173: }
174: }
|