-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathChannelWrapper.java
216 lines (196 loc) · 7.1 KB
/
ChannelWrapper.java
1
2
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package org.redline_rpm;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPSignatureGenerator;
import org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.security.SignatureException;
import java.util.HashMap;
import java.util.Map;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Signature;
import static org.bouncycastle.bcpg.HashAlgorithmTags.SHA1;
import static org.bouncycastle.openpgp.PGPSignature.BINARY_DOCUMENT;
/**
* Wraps an IO channel so that bytes may be observed
* during transmission. Wrappers around IO channels are
* used for a variety of purposes, including counting
* byte output for use in generating headers, calculating
* a signature across output bytes, and digesting output
* bytes using a one-way secure hash.
*/
public abstract class ChannelWrapper {
public static class Key< T> {}
/**
* Interface describing an object that consumes
* data from a NIO buffer.
*/
protected interface Consumer< T> {
/**
* Consume some input from the given buffer.
* @param buffer the buffer to consume
*/
void consume( ByteBuffer buffer);
/**
* Complete operationds and optionally return
* a value to the holder of the key.
* @return reference to the object
*/
T finish();
}
protected Map< Key< ?>, Consumer< ?>> consumers = new HashMap< Key< ?>, Consumer< ?>>();
public Key< Integer> start( final WritableByteChannel output) {
final Key< Integer> object = new Key< Integer>();
consumers.put( object, new Consumer< Integer>() {
int count;
public void consume( final ByteBuffer buffer) {
try {
count += output.write( buffer);
} catch ( IOException e) {
throw new RuntimeException( e);
}
}
public Integer finish() { return count; }
});
return object;
}
/**
* Initializes a byte counter on this channel.
* @return reference to the new key added to the consumers
*/
public Key< Integer> start() {
final Key< Integer> object = new Key< Integer>();
consumers.put( object, new Consumer< Integer>() {
int count;
public void consume( final ByteBuffer buffer) { count += buffer.remaining(); }
public Integer finish() { return count; }
});
return object;
}
/**
* Initialize a signature on this channel.
*
* @param key the private key to use in signing this data stream.
* @return reference to the new key added to the consumers
* @throws NoSuchAlgorithmException if the key algorithm is not supported
* @throws InvalidKeyException if the key provided is invalid for signing
*/
public Key< byte[]> start( final PrivateKey key) throws NoSuchAlgorithmException, InvalidKeyException {
final Signature signature = Signature.getInstance( key.getAlgorithm());
signature.initSign( key);
final Key< byte[]> object = new Key< byte[]>();
consumers.put( object, new Consumer< byte[]>() {
public void consume( final ByteBuffer buffer) {
try {
signature.update( buffer);
} catch ( Exception e) {
throw new RuntimeException( e);
}
}
public byte[] finish() {
try {
return signature.sign();
} catch ( Exception e) {
throw new RuntimeException( e);
}
}
});
return object;
}
/**
* Initialize a PGP signatue on the channel
*
* @param key the private key to use in signing this data stream.
* @param algorithm the algorithm to use. Can be extracted from public key.
* @return reference to the new key added to the consumers
*/
public Key<byte[]> start( final PGPPrivateKey key, int algorithm ) {
BcPGPContentSignerBuilder contentSignerBuilder = new BcPGPContentSignerBuilder( algorithm, SHA1 );
final PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator( contentSignerBuilder );
try {
signatureGenerator.init( BINARY_DOCUMENT, key );
} catch ( PGPException e ) {
throw new RuntimeException( "Could not initialize PGP signature generator", e );
}
final Key<byte[]> object = new Key<byte[]>();
consumers.put( object, new Consumer<byte[]>() {
public void consume( final ByteBuffer buffer ) {
if ( !buffer.hasRemaining() ) {
return;
}
try {
write( buffer );
} catch ( SignatureException e ) {
throw new RuntimeException( "Could not write buffer to PGP signature generator.", e );
}
}
private void write( ByteBuffer buffer ) throws SignatureException {
if ( buffer.hasArray() ) {
byte[] bufferBytes = buffer.array();
int offset = buffer.arrayOffset();
int position = buffer.position();
int limit = buffer.limit();
signatureGenerator.update( bufferBytes, offset + position, limit - position );
buffer.position( limit );
} else {
int length = buffer.remaining();
byte[] bytes = new byte[Util.getTempArraySize( length )];
while ( length > 0 ) {
int chunk = Math.min( length, bytes.length );
buffer.get( bytes, 0, chunk );
signatureGenerator.update( bytes, 0, chunk );
length -= chunk;
}
}
}
public byte[] finish() {
try {
return signatureGenerator.generate().getEncoded();
} catch ( Exception e ) {
throw new RuntimeException( "Could not generate signature.", e );
}
}
});
return object;
}
/**
* Initialize a digest on this channel.
*
* @param algorithm the digest algorithm to use in computing the hash
* @return reference to the new key added to the consumers
* @throws NoSuchAlgorithmException if the given algorithm does not exist
*/
public Key< byte[]> start( final String algorithm) throws NoSuchAlgorithmException {
final MessageDigest digest = MessageDigest.getInstance( algorithm);
final Key< byte[]> object = new Key< byte[]>();
consumers.put( object, new Consumer< byte[]>() {
public void consume( final ByteBuffer buffer) {
try {
digest.update( buffer);
} catch ( Exception e) {
throw new RuntimeException( e);
}
}
public byte[] finish() {
try {
return digest.digest();
} catch ( Exception e) {
throw new RuntimeException( e);
}
}
});
return object;
}
@SuppressWarnings( "unchecked")
public < T> T finish( final Key< T> object) {
return ( T) consumers.remove( object).finish();
}
public void close() throws IOException {
if ( !consumers.isEmpty()) throw new IOException( "There are '" + consumers.size() + "' unfinished operations.");
}
}