01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package edu.iu.uis.eden.security;
18:
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.security.GeneralSecurityException;
22: import java.security.Signature;
23:
24: import javax.servlet.ServletInputStream;
25:
26: /**
27: * An InputStream which decorates another InputStream with a wrapper that verifies the digital signature
28: * of the data after the last piece of data is read. The digital signature to verify against is
29: * passed into the constructor of this stream.
30: *
31: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
32: */
33: public class SignatureVerifyingInputStream extends ServletInputStream {
34:
35: private byte[] digitalSignature;
36: private Signature signature;
37: private InputStream wrappedInputStream;
38:
39: public SignatureVerifyingInputStream(byte[] digitalSignature,
40: Signature signature, InputStream wrappedInputStream) {
41: this .digitalSignature = digitalSignature;
42: this .signature = signature;
43: this .wrappedInputStream = wrappedInputStream;
44: }
45:
46: @Override
47: public synchronized int read() throws IOException {
48: int data = this .wrappedInputStream.read();
49: try {
50: if (data == -1) {
51: verifySignature();
52: } else {
53: this .signature.update((byte) data);
54: }
55: } catch (GeneralSecurityException e) {
56: IOException exception = new IOException(
57: "Error processing digital signature.");
58: exception.initCause(e);
59: throw exception;
60: }
61: return data;
62: }
63:
64: protected void verifySignature() throws IOException,
65: GeneralSecurityException {
66: boolean verifies = this .signature.verify(this .digitalSignature);
67: if (!verifies) {
68: throw new IOException(
69: "The digital signature could not be successfully verified!");
70: }
71: }
72:
73: }
|