|
31 | 31 |
|
32 | 32 | import java.io.File; |
33 | 33 | import java.io.FileInputStream; |
| 34 | +import java.io.FileNotFoundException; |
34 | 35 | import java.io.IOException; |
35 | 36 | import java.io.InputStream; |
36 | 37 |
|
37 | 38 | import org.apache.commons.compress.utils.IOUtils; |
| 39 | +import org.bouncycastle.bcpg.ArmoredInputStream; |
38 | 40 | import org.bouncycastle.openpgp.PGPException; |
39 | 41 | import org.bouncycastle.openpgp.PGPObjectFactory; |
40 | 42 | import org.bouncycastle.openpgp.PGPPublicKey; |
41 | 43 | import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; |
42 | 44 | import org.bouncycastle.openpgp.PGPSignature; |
43 | 45 | import org.bouncycastle.openpgp.PGPSignatureList; |
44 | 46 | import org.bouncycastle.openpgp.PGPUtil; |
| 47 | +import org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory; |
45 | 48 | import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; |
46 | 49 | import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider; |
47 | 50 |
|
@@ -127,4 +130,70 @@ private PGPPublicKey readPublicKey(long id) throws IOException, PGPException { |
127 | 130 | } |
128 | 131 | } |
129 | 132 |
|
| 133 | + public String[] extractTextFromCleartextSignature(File inFile) throws FileNotFoundException, IOException { |
| 134 | + try (ArmoredInputStream in = new ArmoredInputStream(new FileInputStream(inFile))) { |
| 135 | + return extractTextFromCleartextSignature(in); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public boolean verifyCleartextSignature(File inFile) { |
| 140 | + try (ArmoredInputStream in = new ArmoredInputStream(new FileInputStream(inFile))) { |
| 141 | + String[] clearTextLines = extractTextFromCleartextSignature(in); |
| 142 | + int clearTextSize = clearTextLines.length; |
| 143 | + |
| 144 | + JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(in); |
| 145 | + PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject(); |
| 146 | + PGPSignature sig = p3.get(0); |
| 147 | + PGPPublicKey publicKey = readPublicKey(sig.getKeyID()); |
| 148 | + |
| 149 | + sig.init(new BcPGPContentVerifierBuilderProvider(), publicKey); |
| 150 | + for (int i = 0; i < clearTextSize; i++) { |
| 151 | + sig.update(clearTextLines[i].getBytes()); |
| 152 | + if (i + 1 < clearTextSize) { |
| 153 | + // https://tools.ietf.org/html/rfc4880#section-7 |
| 154 | + // Convert all line endings to '\r\n' |
| 155 | + sig.update((byte) '\r'); |
| 156 | + sig.update((byte) '\n'); |
| 157 | + } |
| 158 | + } |
| 159 | + return sig.verify(); |
| 160 | + } catch (Exception e) { |
| 161 | + e.printStackTrace(); |
| 162 | + return false; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private String[] extractTextFromCleartextSignature(ArmoredInputStream in) throws FileNotFoundException, IOException { |
| 167 | + // https://tools.ietf.org/html/rfc4880#section-7 |
| 168 | + // ArmoredInputStream does unescape dash-escaped string in armored text and skips |
| 169 | + // all headers. To calculate the signature we still need to: |
| 170 | + // 1. handle different line endings \n or \n\r or \r\n |
| 171 | + // 2. remove trailing whitespaces from each line (' ' and '\t') |
| 172 | + // 3. remove the latest line ending |
| 173 | + |
| 174 | + String clearText = ""; |
| 175 | + for (;;) { |
| 176 | + int c = in.read(); |
| 177 | + // in.isClearText() refers to the PREVIOUS byte read |
| 178 | + if (c == -1 || !in.isClearText()) { |
| 179 | + break; |
| 180 | + } |
| 181 | + // 1. convert all line endings to '\r\n' |
| 182 | + if (c == '\r') { |
| 183 | + continue; |
| 184 | + } |
| 185 | + clearText += (char) c; |
| 186 | + } |
| 187 | + |
| 188 | + // 3. remove the latest line ending |
| 189 | + if (clearText.endsWith("\n")) { |
| 190 | + clearText = clearText.substring(0, clearText.length() - 1); |
| 191 | + } |
| 192 | + String[] lines = clearText.split("\n", -1); |
| 193 | + for (int i = 0; i < lines.length; i++) { |
| 194 | + // 2. remove trailing whitespaces from each line (' ' and '\t') |
| 195 | + lines[i] = lines[i].replaceAll("[ \\t]+$", ""); |
| 196 | + } |
| 197 | + return lines; |
| 198 | + } |
130 | 199 | } |
0 commit comments