001: package org.bouncycastle.openpgp.examples.test;
002:
003: import junit.framework.Test;
004: import junit.framework.TestCase;
005: import junit.framework.TestSuite;
006: import org.bouncycastle.openpgp.examples.ClearSignedFileProcessor;
007: import org.bouncycastle.openpgp.examples.DSAElGamalKeyRingGenerator;
008: import org.bouncycastle.openpgp.examples.KeyBasedFileProcessor;
009: import org.bouncycastle.openpgp.examples.KeyBasedLargeFileProcessor;
010: import org.bouncycastle.openpgp.examples.PBEFileProcessor;
011: import org.bouncycastle.openpgp.examples.RSAKeyPairGenerator;
012: import org.bouncycastle.openpgp.examples.SignedFileProcessor;
013: import org.bouncycastle.util.encoders.Base64;
014:
015: import java.io.BufferedReader;
016: import java.io.BufferedWriter;
017: import java.io.ByteArrayInputStream;
018: import java.io.ByteArrayOutputStream;
019: import java.io.FileOutputStream;
020: import java.io.FileWriter;
021: import java.io.IOException;
022: import java.io.InputStreamReader;
023: import java.io.PrintStream;
024:
025: public class AllTests extends TestCase {
026: byte[] clearSignedPublicKey = Base64
027: .decode("mQELBEQh2+wBCAD26kte0hO6flr7Y2aetpPYutHY4qsmDPy+GwmmqVeCDkX+"
028: + "r1g7DuFbMhVeu0NkKDnVl7GsJ9VarYsFYyqu0NzLa9XS2qlTIkmJV+2/xKa1"
029: + "tzjn18fT/cnAWL88ZLCOWUr241aPVhLuIc6vpHnySpEMkCh4rvMaimnTrKwO"
030: + "42kgeDGd5cXfs4J4ovRcTbc4hmU2BRVsRjiYMZWWx0kkyL2zDVyaJSs4yVX7"
031: + "Jm4/LSR1uC/wDT0IJJuZT/gQPCMJNMEsVCziRgYkAxQK3OWojPSuv4rXpyd4"
032: + "Gvo6IbvyTgIskfpSkCnQtORNLIudQSuK7pW+LkL62N+ohuKdMvdxauOnAAYp"
033: + "tBNnZ2dnZ2dnZyA8Z2dnQGdnZ2c+iQE2BBMBAgAgBQJEIdvsAhsDBgsJCAcD"
034: + "AgQVAggDBBYCAwECHgECF4AACgkQ4M/Ier3f9xagdAf/fbKWBjLQM8xR7JkR"
035: + "P4ri8YKOQPhK+VrddGUD59/wzVnvaGyl9MZE7TXFUeniQq5iXKnm22EQbYch"
036: + "v2Jcxyt2H9yptpzyh4tP6tEHl1C887p2J4qe7F2ATua9CzVGwXQSUbKtj2fg"
037: + "UZP5SsNp25guhPiZdtkf2sHMeiotmykFErzqGMrvOAUThrO63GiYsRk4hF6r"
038: + "cQ01d+EUVpY/sBcCxgNyOiB7a84sDtrxnX5BTEZDTEj8LvuEyEV3TMUuAjx1"
039: + "7Eyd+9JtKzwV4v3hlTaWOvGro9nPS7YaPuG+RtufzXCUJPbPfTjTvtGOqvEz"
040: + "oztls8tuWA0OGHba9XfX9rfgorACAAM=");
041:
042: String crOnlyMessage = "\r" + " hello world!\r" + "\r" + "- dash\r";
043:
044: String nlOnlyMessage = "\n" + " hello world!\n" + "\n" + "- dash\n";
045:
046: String crNlMessage = "\r\n" + " hello world!\r\n" + "\r\n"
047: + "- dash\r\n";
048:
049: String crNlMessageTrailingWhiteSpace = "\r\n"
050: + " hello world! \t\r\n" + "\r\n" + "\r\n";
051:
052: String crOnlySignedMessage = "-----BEGIN PGP SIGNED MESSAGE-----\r"
053: + "Hash: SHA256\r"
054: + "\r"
055: + "\r"
056: + " hello world!\r"
057: + "\r"
058: + "- - dash\r"
059: + "-----BEGIN PGP SIGNATURE-----\r"
060: + "Version: GnuPG v1.4.2.1 (GNU/Linux)\r"
061: + "\r"
062: + "iQEVAwUBRCNS8+DPyHq93/cWAQi6SwgAj3ItmSLr/sd/ixAQLW7/12jzEjfNmFDt\r"
063: + "WOZpJFmXj0fnMzTrOILVnbxHv2Ru+U8Y1K6nhzFSR7d28n31/XGgFtdohDEaFJpx\r"
064: + "Fl+KvASKIonnpEDjFJsPIvT1/G/eCPalwO9IuxaIthmKj0z44SO1VQtmNKxdLAfK\r"
065: + "+xTnXGawXS1WUE4CQGPM45mIGSqXcYrLtJkAg3jtRa8YRUn2d7b2BtmWH+jVaVuC\r"
066: + "hNrXYv7iHFOu25yRWhUQJisvdC13D/gKIPRvARXPgPhAC2kovIy6VS8tDoyG6Hm5\r"
067: + "dMgLEGhmqsgaetVq1ZIuBZj5S4j2apBJCDpF6GBfpBOfwIZs0Tpmlw==\r"
068: + "=84Nd\r" + "-----END PGP SIGNATURE-----\r";
069:
070: String nlOnlySignedMessage = "-----BEGIN PGP SIGNED MESSAGE-----\n"
071: + "Hash: SHA256\n"
072: + "\n"
073: + "\n"
074: + " hello world!\n"
075: + "\n"
076: + "- - dash\n"
077: + "-----BEGIN PGP SIGNATURE-----\n"
078: + "Version: GnuPG v1.4.2.1 (GNU/Linux)\n"
079: + "\n"
080: + "iQEVAwUBRCNS8+DPyHq93/cWAQi6SwgAj3ItmSLr/sd/ixAQLW7/12jzEjfNmFDt\n"
081: + "WOZpJFmXj0fnMzTrOILVnbxHv2Ru+U8Y1K6nhzFSR7d28n31/XGgFtdohDEaFJpx\n"
082: + "Fl+KvASKIonnpEDjFJsPIvT1/G/eCPalwO9IuxaIthmKj0z44SO1VQtmNKxdLAfK\n"
083: + "+xTnXGawXS1WUE4CQGPM45mIGSqXcYrLtJkAg3jtRa8YRUn2d7b2BtmWH+jVaVuC\n"
084: + "hNrXYv7iHFOu25yRWhUQJisvdC13D/gKIPRvARXPgPhAC2kovIy6VS8tDoyG6Hm5\n"
085: + "dMgLEGhmqsgaetVq1ZIuBZj5S4j2apBJCDpF6GBfpBOfwIZs0Tpmlw==\n"
086: + "=84Nd\n" + "-----END PGP SIGNATURE-----\n";
087:
088: String crNlSignedMessage = "-----BEGIN PGP SIGNED MESSAGE-----\r\n"
089: + "Hash: SHA256\r\n"
090: + "\r\n"
091: + "\r\n"
092: + " hello world!\r\n"
093: + "\r\n"
094: + "- - dash\r\n"
095: + "-----BEGIN PGP SIGNATURE-----\r\n"
096: + "Version: GnuPG v1.4.2.1 (GNU/Linux)\r\n"
097: + "\r\n"
098: + "iQEVAwUBRCNS8+DPyHq93/cWAQi6SwgAj3ItmSLr/sd/ixAQLW7/12jzEjfNmFDt\r\n"
099: + "WOZpJFmXj0fnMzTrOILVnbxHv2Ru+U8Y1K6nhzFSR7d28n31/XGgFtdohDEaFJpx\r\n"
100: + "Fl+KvASKIonnpEDjFJsPIvT1/G/eCPalwO9IuxaIthmKj0z44SO1VQtmNKxdLAfK\r\n"
101: + "+xTnXGawXS1WUE4CQGPM45mIGSqXcYrLtJkAg3jtRa8YRUn2d7b2BtmWH+jVaVuC\r\n"
102: + "hNrXYv7iHFOu25yRWhUQJisvdC13D/gKIPRvARXPgPhAC2kovIy6VS8tDoyG6Hm5\r\n"
103: + "dMgLEGhmqsgaetVq1ZIuBZj5S4j2apBJCDpF6GBfpBOfwIZs0Tpmlw==\r\n"
104: + "=84Nd\r" + "-----END PGP SIGNATURE-----\r\n";
105:
106: String crNlSignedMessageTrailingWhiteSpace = "-----BEGIN PGP SIGNED MESSAGE-----\r\n"
107: + "Hash: SHA256\r\n"
108: + "\r\n"
109: + "\r\n"
110: + " hello world! \t\r\n"
111: + "\r\n"
112: + "- - dash\r\n"
113: + "-----BEGIN PGP SIGNATURE-----\r\n"
114: + "Version: GnuPG v1.4.2.1 (GNU/Linux)\r\n"
115: + "\r\n"
116: + "iQEVAwUBRCNS8+DPyHq93/cWAQi6SwgAj3ItmSLr/sd/ixAQLW7/12jzEjfNmFDt\r\n"
117: + "WOZpJFmXj0fnMzTrOILVnbxHv2Ru+U8Y1K6nhzFSR7d28n31/XGgFtdohDEaFJpx\r\n"
118: + "Fl+KvASKIonnpEDjFJsPIvT1/G/eCPalwO9IuxaIthmKj0z44SO1VQtmNKxdLAfK\r\n"
119: + "+xTnXGawXS1WUE4CQGPM45mIGSqXcYrLtJkAg3jtRa8YRUn2d7b2BtmWH+jVaVuC\r\n"
120: + "hNrXYv7iHFOu25yRWhUQJisvdC13D/gKIPRvARXPgPhAC2kovIy6VS8tDoyG6Hm5\r\n"
121: + "dMgLEGhmqsgaetVq1ZIuBZj5S4j2apBJCDpF6GBfpBOfwIZs0Tpmlw==\r\n"
122: + "=84Nd\r" + "-----END PGP SIGNATURE-----\r\n";
123:
124: private PrintStream _oldOut;
125: private PrintStream _oldErr;
126:
127: private ByteArrayOutputStream _currentOut;
128: private ByteArrayOutputStream _currentErr;
129:
130: public void setUp() throws Exception {
131: _oldOut = System.out;
132: _oldErr = System.err;
133: _currentOut = new ByteArrayOutputStream();
134: _currentErr = new ByteArrayOutputStream();
135:
136: System.setOut(new PrintStream(_currentOut));
137: System.setErr(new PrintStream(_currentErr));
138: }
139:
140: public void tearDown() {
141: System.setOut(_oldOut);
142: System.setErr(_oldErr);
143: }
144:
145: public void testRSAKeyGeneration() throws Exception {
146: RSAKeyPairGenerator.main(new String[] { "test", "password" });
147:
148: createSmallTestInput();
149: createLargeTestInput();
150:
151: checkSigning("bpg");
152: checkKeyBasedEncryption("bpg");
153: checkLargeKeyBasedEncryption("bpg");
154:
155: RSAKeyPairGenerator.main(new String[] { "-a", "test",
156: "password" });
157:
158: checkSigning("asc");
159: checkKeyBasedEncryption("asc");
160: checkLargeKeyBasedEncryption("asc");
161: }
162:
163: public void testDSAElGamaleKeyGeneration() throws Exception {
164: DSAElGamalKeyRingGenerator.main(new String[] { "test",
165: "password" });
166:
167: createSmallTestInput();
168: createLargeTestInput();
169:
170: checkSigning("bpg");
171: checkKeyBasedEncryption("bpg");
172: checkLargeKeyBasedEncryption("bpg");
173:
174: DSAElGamalKeyRingGenerator.main(new String[] { "-a", "test",
175: "password" });
176:
177: checkSigning("asc");
178: checkKeyBasedEncryption("asc");
179: checkLargeKeyBasedEncryption("asc");
180: }
181:
182: public void testPBEEncryption() throws Exception {
183: _currentErr.reset();
184:
185: PBEFileProcessor.main(new String[] { "-e", "test.txt",
186: "password" });
187:
188: PBEFileProcessor.main(new String[] { "-d", "test.txt.bpg",
189: "password" });
190:
191: assertEquals("no message integrity check", getLine(_currentErr));
192:
193: PBEFileProcessor.main(new String[] { "-e", "-i", "test.txt",
194: "password" });
195:
196: PBEFileProcessor.main(new String[] { "-d", "test.txt.bpg",
197: "password" });
198:
199: assertEquals("message integrity check passed",
200: getLine(_currentErr));
201:
202: PBEFileProcessor.main(new String[] { "-e", "-ai", "test.txt",
203: "password" });
204:
205: PBEFileProcessor.main(new String[] { "-d", "test.txt.asc",
206: "password" });
207:
208: assertEquals("message integrity check passed",
209: getLine(_currentErr));
210: }
211:
212: public void testClearSigned() throws Exception {
213: createTestKey(clearSignedPublicKey, "pub.bpg");
214:
215: checkClearSignedVerify(nlOnlySignedMessage);
216: checkClearSignedVerify(crOnlySignedMessage);
217: checkClearSignedVerify(crNlSignedMessage);
218: checkClearSignedVerify(crNlSignedMessageTrailingWhiteSpace);
219:
220: ClearSignedFileProcessor.main(new String[] { "-v",
221: "test.txt.asc", "pub.bpg" });
222:
223: RSAKeyPairGenerator.main(new String[] { "test", "password" });
224:
225: checkClearSigned(crOnlyMessage);
226: checkClearSigned(nlOnlyMessage);
227: checkClearSigned(crNlMessage);
228: checkClearSigned(crNlMessageTrailingWhiteSpace);
229: }
230:
231: private void checkClearSignedVerify(String message)
232: throws Exception {
233: createTestData(message, "test.txt.asc");
234:
235: ClearSignedFileProcessor.main(new String[] { "-v",
236: "test.txt.asc", "pub.bpg" });
237: }
238:
239: private void checkClearSigned(String message) throws Exception {
240: createTestData(message, "test.txt");
241:
242: ClearSignedFileProcessor.main(new String[] { "-s", "test.txt",
243: "secret.bpg", "password" });
244: ClearSignedFileProcessor.main(new String[] { "-v",
245: "test.txt.asc", "pub.bpg" });
246: }
247:
248: private void checkSigning(String type) throws Exception {
249: _currentOut.reset();
250:
251: SignedFileProcessor.main(new String[] { "-s", "test.txt",
252: "secret." + type, "password" });
253:
254: SignedFileProcessor.main(new String[] { "-v", "test.txt.bpg",
255: "pub." + type });
256:
257: assertEquals("signature verified.", getLine(_currentOut));
258:
259: SignedFileProcessor.main(new String[] { "-s", "-a", "test.txt",
260: "secret." + type, "password" });
261:
262: SignedFileProcessor.main(new String[] { "-v", "test.txt.asc",
263: "pub." + type });
264:
265: assertEquals("signature verified.", getLine(_currentOut));
266: }
267:
268: private void checkKeyBasedEncryption(String type) throws Exception {
269: _currentErr.reset();
270:
271: KeyBasedFileProcessor.main(new String[] { "-e", "test.txt",
272: "pub." + type });
273:
274: KeyBasedFileProcessor.main(new String[] { "-d", "test.txt.bpg",
275: "secret." + type, "password" });
276:
277: assertEquals("no message integrity check", getLine(_currentErr));
278:
279: KeyBasedFileProcessor.main(new String[] { "-e", "-i",
280: "test.txt", "pub." + type });
281:
282: KeyBasedFileProcessor.main(new String[] { "-d", "test.txt.bpg",
283: "secret." + type, "password" });
284:
285: assertEquals("message integrity check passed",
286: getLine(_currentErr));
287:
288: KeyBasedFileProcessor.main(new String[] { "-e", "-ai",
289: "test.txt", "pub." + type });
290:
291: KeyBasedFileProcessor.main(new String[] { "-d", "test.txt.asc",
292: "secret." + type, "password" });
293:
294: assertEquals("message integrity check passed",
295: getLine(_currentErr));
296: }
297:
298: private void checkLargeKeyBasedEncryption(String type)
299: throws Exception {
300: _currentErr.reset();
301:
302: KeyBasedLargeFileProcessor.main(new String[] { "-e",
303: "large.txt", "pub." + type });
304:
305: KeyBasedLargeFileProcessor.main(new String[] { "-d",
306: "large.txt.bpg", "secret." + type, "password" });
307:
308: assertEquals("no message integrity check", getLine(_currentErr));
309:
310: KeyBasedLargeFileProcessor.main(new String[] { "-e", "-i",
311: "large.txt", "pub." + type });
312:
313: KeyBasedLargeFileProcessor.main(new String[] { "-d",
314: "large.txt.bpg", "secret." + type, "password" });
315:
316: assertEquals("message integrity check passed",
317: getLine(_currentErr));
318:
319: KeyBasedLargeFileProcessor.main(new String[] { "-e", "-ai",
320: "large.txt", "pub." + type });
321:
322: KeyBasedLargeFileProcessor.main(new String[] { "-d",
323: "large.txt.asc", "secret." + type, "password" });
324:
325: assertEquals("message integrity check passed",
326: getLine(_currentErr));
327: }
328:
329: private void createSmallTestInput() throws IOException {
330: BufferedWriter bfOut = new BufferedWriter(new FileWriter(
331: "test.txt"));
332:
333: bfOut.write("hello world!");
334: bfOut.newLine();
335:
336: bfOut.close();
337: }
338:
339: private void createLargeTestInput() throws IOException {
340: BufferedWriter bfOut = new BufferedWriter(new FileWriter(
341: "large.txt"));
342:
343: for (int i = 0; i != 2000; i++) {
344: bfOut.write("hello world!");
345: bfOut.newLine();
346: }
347:
348: bfOut.close();
349: }
350:
351: private void createTestData(String testData, String name)
352: throws IOException {
353: BufferedWriter bfOut = new BufferedWriter(new FileWriter(name));
354:
355: bfOut.write(testData);
356:
357: bfOut.close();
358: }
359:
360: private void createTestKey(byte[] keyData, String name)
361: throws IOException {
362: FileOutputStream fOut = new FileOutputStream(name);
363:
364: fOut.write(keyData);
365:
366: fOut.close();
367: }
368:
369: private String getLine(ByteArrayOutputStream out)
370: throws IOException {
371: BufferedReader bRd = new BufferedReader(new InputStreamReader(
372: new ByteArrayInputStream(out.toByteArray())));
373:
374: out.reset();
375:
376: return bRd.readLine();
377: }
378:
379: public static void main(String[] args) {
380: junit.textui.TestRunner.run(suite());
381: }
382:
383: public static Test suite() {
384: TestSuite suite = new TestSuite("OpenPGP Example Tests");
385:
386: suite.addTestSuite(AllTests.class);
387:
388: return suite;
389: }
390: }
|