001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Boris V. Kuznetsov
020: * @version $Revision$
021: */package java.security;
022:
023: import java.io.ByteArrayInputStream;
024: import java.io.ByteArrayOutputStream;
025: import java.io.IOException;
026: import java.io.ObjectInputStream;
027: import java.io.ObjectOutputStream;
028: import java.io.Serializable;
029:
030: /**
031: * @com.intel.drl.spec_ref
032: *
033: */
034: public final class SignedObject implements Serializable {
035:
036: /**
037: * @com.intel.drl.spec_ref
038: */
039: private static final long serialVersionUID = 720502720485447167L;
040:
041: /**
042: * @com.intel.drl.spec_ref
043: */
044: private byte[] content;
045:
046: /**
047: * @com.intel.drl.spec_ref
048: */
049: private byte[] signature;
050:
051: /**
052: * @com.intel.drl.spec_ref
053: */
054: private String thealgorithm;
055:
056: /**
057: * @com.intel.drl.spec_ref
058: *
059: */
060: private void readObject(ObjectInputStream s) throws IOException,
061: ClassNotFoundException {
062:
063: s.defaultReadObject();
064: byte[] tmp = new byte[content.length];
065: System.arraycopy(content, 0, tmp, 0, content.length);
066: content = tmp;
067: tmp = new byte[signature.length];
068: System.arraycopy(signature, 0, tmp, 0, signature.length);
069: signature = tmp;
070: }
071:
072: /**
073: * @com.intel.drl.spec_ref
074: *
075: */
076: public SignedObject(Serializable object, PrivateKey signingKey,
077: Signature signingEngine) throws IOException,
078: InvalidKeyException, SignatureException {
079:
080: ByteArrayOutputStream baos = new ByteArrayOutputStream();
081: ObjectOutputStream oos = new ObjectOutputStream(baos);
082: try {
083: // Serialize
084: oos.writeObject(object);
085: oos.flush();
086: } finally {
087: oos.close();
088: }
089: content = baos.toByteArray();
090: signingEngine.initSign(signingKey);
091: thealgorithm = signingEngine.getAlgorithm();
092: signingEngine.update(content);
093: signature = signingEngine.sign();
094: }
095:
096: /**
097: * @com.intel.drl.spec_ref
098: *
099: */
100: public Object getObject() throws IOException,
101: ClassNotFoundException {
102: // deserialize our object
103: ObjectInputStream ois = new ObjectInputStream(
104: new ByteArrayInputStream(content));
105: try {
106: return ois.readObject();
107: } finally {
108: ois.close();
109: }
110: }
111:
112: /**
113: * @com.intel.drl.spec_ref
114: *
115: */
116: public byte[] getSignature() {
117: byte[] sig = new byte[signature.length];
118: System.arraycopy(signature, 0, sig, 0, signature.length);
119: return sig;
120: }
121:
122: /**
123: * @com.intel.drl.spec_ref
124: *
125: */
126: public String getAlgorithm() {
127: return thealgorithm;
128: }
129:
130: /**
131: * @com.intel.drl.spec_ref
132: *
133: */
134: public boolean verify(PublicKey verificationKey,
135: Signature verificationEngine) throws InvalidKeyException,
136: SignatureException {
137:
138: verificationEngine.initVerify(verificationKey);
139: verificationEngine.update(content);
140: return verificationEngine.verify(signature);
141: }
142:
143: }
|