1 | |
package org.apache.commons.openpgp; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
import java.io.IOException; |
21 | |
import java.io.InputStream; |
22 | |
import java.security.Security; |
23 | |
import java.security.SignatureException; |
24 | |
|
25 | |
import org.bouncycastle.jce.provider.BouncyCastleProvider; |
26 | |
import org.bouncycastle.openpgp.PGPCompressedData; |
27 | |
import org.bouncycastle.openpgp.PGPException; |
28 | |
import org.bouncycastle.openpgp.PGPObjectFactory; |
29 | |
import org.bouncycastle.openpgp.PGPPublicKey; |
30 | |
import org.bouncycastle.openpgp.PGPSignature; |
31 | |
import org.bouncycastle.openpgp.PGPSignatureList; |
32 | |
import org.bouncycastle.openpgp.PGPUtil; |
33 | |
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class BouncyCastleOpenPgpStreamingSignatureVerifier |
41 | |
implements OpenPgpStreamingSignatureVerifier |
42 | |
{ |
43 | |
private PGPSignature sig; |
44 | |
|
45 | |
public BouncyCastleOpenPgpStreamingSignatureVerifier( InputStream signature, KeyRing keyRing ) |
46 | |
throws OpenPgpException, IOException |
47 | 20 | { |
48 | 20 | init( signature, keyRing ); |
49 | 18 | } |
50 | |
|
51 | |
private void init( InputStream signature, KeyRing keyRing ) |
52 | |
throws OpenPgpException, IOException |
53 | |
{ |
54 | |
|
55 | 20 | Security.addProvider( new BouncyCastleProvider() ); |
56 | |
|
57 | |
try |
58 | |
{ |
59 | 20 | signature = PGPUtil.getDecoderStream( signature ); |
60 | |
|
61 | 20 | PGPPublicKey key = null; |
62 | 41 | while ( key == null && signature.available() > 0 ) |
63 | |
{ |
64 | 21 | PGPObjectFactory pgpFact = new PGPObjectFactory( signature ); |
65 | |
|
66 | |
PGPSignatureList p3; |
67 | |
|
68 | 21 | Object o = pgpFact.nextObject(); |
69 | 21 | if ( o == null ) |
70 | |
{ |
71 | 0 | break; |
72 | |
} |
73 | |
|
74 | 21 | if ( o instanceof PGPCompressedData ) |
75 | |
{ |
76 | 0 | PGPCompressedData c1 = (PGPCompressedData) o; |
77 | |
|
78 | 0 | pgpFact = new PGPObjectFactory( c1.getDataStream() ); |
79 | |
|
80 | 0 | p3 = (PGPSignatureList) pgpFact.nextObject(); |
81 | 0 | } |
82 | |
else |
83 | |
{ |
84 | 21 | p3 = (PGPSignatureList) o; |
85 | |
} |
86 | |
|
87 | 26 | for ( int i = 0; i < p3.size(); i++ ) |
88 | |
{ |
89 | 23 | sig = p3.get( i ); |
90 | 23 | key = keyRing.getPublicKey( sig.getKeyID() ); |
91 | 23 | if ( key != null ) |
92 | |
{ |
93 | 18 | break; |
94 | |
} |
95 | |
else |
96 | |
{ |
97 | |
|
98 | |
} |
99 | |
} |
100 | |
|
101 | 21 | } |
102 | |
|
103 | 20 | if ( key == null ) |
104 | |
{ |
105 | 2 | throw new UnknownKeyException( "Unable to find key with key ID '" |
106 | |
+ Long.toHexString( sig.getKeyID() ).toUpperCase() + "' in public key ring" ); |
107 | |
} |
108 | |
|
109 | 18 | sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), key); |
110 | |
} |
111 | 0 | catch ( PGPException e ) |
112 | |
{ |
113 | |
|
114 | 0 | throw new OpenPgpException( "Error calculating detached signature", e ); |
115 | 18 | } |
116 | 18 | } |
117 | |
|
118 | |
public void update( byte[] buf ) |
119 | |
throws OpenPgpException |
120 | |
{ |
121 | 0 | update( buf, 0, buf.length ); |
122 | 0 | } |
123 | |
|
124 | |
public void update( byte[] buf, int offset, int length ) |
125 | |
throws OpenPgpException |
126 | |
{ |
127 | |
try |
128 | |
{ |
129 | 18 | sig.update( buf, offset, length ); |
130 | |
} |
131 | 0 | catch ( SignatureException e ) |
132 | |
{ |
133 | |
|
134 | 0 | throw new OpenPgpException( "Error calculating detached signature", e ); |
135 | 18 | } |
136 | 18 | } |
137 | |
|
138 | |
public SignatureStatus finish() |
139 | |
throws OpenPgpException, IOException |
140 | |
{ |
141 | |
try |
142 | |
{ |
143 | 18 | if ( sig.verify() ) |
144 | |
{ |
145 | |
|
146 | 16 | return SignatureStatus.VALID_UNTRUSTED; |
147 | |
} |
148 | |
else |
149 | |
{ |
150 | 2 | return SignatureStatus.INVALID; |
151 | |
} |
152 | |
} |
153 | 0 | catch ( PGPException e ) |
154 | |
{ |
155 | |
|
156 | 0 | throw new OpenPgpException( "Error calculating detached signature", e ); |
157 | |
} |
158 | 0 | catch ( SignatureException e ) |
159 | |
{ |
160 | |
|
161 | 0 | throw new OpenPgpException( "Error calculating detached signature", e ); |
162 | |
} |
163 | |
} |
164 | |
} |