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.core.node;
028:
029: import java.security.PrivateKey;
030: import java.security.PublicKey;
031: import java.security.Signature;
032: import java.security.SignedObject;
033:
034: import org.cougaar.core.mts.Message;
035: import org.cougaar.core.mts.MessageSecurityManager;
036:
037: /**
038: * A useful {@link MessageSecurityManager} which signs each message,
039: * but depends on the destination to recover the public key of the
040: * sender.
041: */
042: public class SignedMessageSecurityManager implements
043: MessageSecurityManager {
044:
045: public SignedMessageSecurityManager() {
046: }
047:
048: public Message secureMessage(Message m) {
049: return new SignedSecureMessage(m);
050: }
051:
052: public Message unsecureMessage(SecureMessage m) {
053: if (m instanceof SignedSecureMessage) {
054: return ((SignedSecureMessage) m).extract();
055: } else {
056: return null;
057: }
058: }
059:
060: private static class SignedSecureMessage extends Message implements
061: SecureMessage {
062: private SignedObject secret;
063:
064: //private java.security.cert.Certificate cert;
065:
066: SignedSecureMessage(Message m) {
067: super (m.getOriginator(), m.getTarget());
068: secret = SignedMessageSecurityManager.sign(m);
069: //cert = SignedMessageSecurityManager.getCert(origin.getAddress());
070: }
071:
072: Message extract() {
073: try {
074: java.security.cert.Certificate cert = KeyRing
075: .getCert(getOriginator().getAddress());
076: if (cert == null) {
077: System.err
078: .println("\nWarning: Dropping message, No public certificate for Origin \""
079: + getOriginator().getAddress()
080: + "\": " + secret.getObject());
081: return (Message) secret.getObject();
082: }
083: if (verify(secret, cert)) {
084: return (Message) secret.getObject();
085: } else {
086: return null;
087: }
088: } catch (Exception e) {
089: e.printStackTrace();
090: return null;
091: }
092: }
093: }
094:
095: static SignedObject sign(Message m) {
096: try {
097: String origin = m.getOriginator().getAddress();
098: PrivateKey pk = KeyRing.getPrivateKey(origin);
099: if (pk == null) {
100: System.err
101: .println("\nWarning: Dropping message, Could not find private key for Origin \""
102: + origin + "\": " + m);
103: return null;
104: }
105: Signature se = Signature.getInstance(pk.getAlgorithm());
106: return new SignedObject(m, pk, se);
107: } catch (Exception e) {
108: e.printStackTrace();
109: throw new RuntimeException(e.toString());
110: }
111: }
112:
113: static boolean verify(SignedObject so,
114: java.security.cert.Certificate cert) {
115: // check for bogus conditions.
116: if (so == null || cert == null)
117: return false;
118: try {
119: PublicKey pk = cert.getPublicKey();
120: Signature ve = Signature.getInstance(so.getAlgorithm());
121: return so.verify(pk, ve);
122: } catch (Exception e) {
123: e.printStackTrace();
124: }
125: return false;
126: }
127:
128: }
|