001: package org.bouncycastle.openssl.test;
002:
003: import org.bouncycastle.jce.provider.BouncyCastleProvider;
004: import org.bouncycastle.openssl.PEMReader;
005: import org.bouncycastle.openssl.PEMWriter;
006: import org.bouncycastle.openssl.PasswordFinder;
007: import org.bouncycastle.util.test.SimpleTest;
008:
009: import java.io.IOException;
010: import java.io.StringReader;
011: import java.io.StringWriter;
012: import java.math.BigInteger;
013: import java.security.KeyFactory;
014: import java.security.KeyPair;
015: import java.security.KeyPairGenerator;
016: import java.security.PrivateKey;
017: import java.security.SecureRandom;
018: import java.security.Security;
019: import java.security.spec.DSAParameterSpec;
020: import java.security.spec.RSAPrivateCrtKeySpec;
021:
022: public class WriterTest extends SimpleTest {
023: private static final SecureRandom random = new SecureRandom();
024:
025: // TODO Replace with a randomly generated key each test run?
026: private static final RSAPrivateCrtKeySpec testRsaKeySpec = new RSAPrivateCrtKeySpec(
027: new BigInteger(
028: "b4a7e46170574f16a97082b22be58b6a2a629798419be12872a4bdba626cfae9900f76abfb12139dce5de56564fab2b6543165a040c606887420e33d91ed7ed7",
029: 16),
030: new BigInteger("11", 16),
031: new BigInteger(
032: "9f66f6b05410cd503b2709e88115d55daced94d1a34d4e32bf824d0dde6028ae79c5f07b580f5dce240d7111f7ddb130a7945cd7d957d1920994da389f490c89",
033: 16),
034: new BigInteger(
035: "c0a0758cdf14256f78d4708c86becdead1b50ad4ad6c5c703e2168fbf37884cb",
036: 16),
037: new BigInteger(
038: "f01734d7960ea60070f1b06f2bb81bfac48ff192ae18451d5e56c734a5aab8a5",
039: 16),
040: new BigInteger(
041: "b54bb9edff22051d9ee60f9351a48591b6500a319429c069a3e335a1d6171391",
042: 16),
043: new BigInteger(
044: "d3d83daf2a0cecd3367ae6f8ae1aeb82e9ac2f816c6fc483533d8297dd7884cd",
045: 16),
046: new BigInteger(
047: "b8f52fc6f38593dabb661d3f50f8897f8106eee68b1bce78a95b132b4e5b5d19",
048: 16));
049:
050: private static final DSAParameterSpec testDsaParams = new DSAParameterSpec(
051: new BigInteger(
052: "7434410770759874867539421675728577177024889699586189000788950934679315164676852047058354758883833299702695428196962057871264685291775577130504050839126673"),
053: new BigInteger(
054: "1138656671590261728308283492178581223478058193247"),
055: new BigInteger(
056: "4182906737723181805517018315469082619513954319976782448649747742951189003482834321192692620856488639629011570381138542789803819092529658402611668375788410"));
057:
058: private static final char[] testPassword = "bouncy".toCharArray();
059:
060: private static final String[] algorithms = new String[] {
061: "AES-128-CBC", "AES-128-CFB", "AES-128-ECB", "AES-128-OFB",
062: "AES-192-CBC", "AES-192-CFB", "AES-192-ECB", "AES-192-OFB",
063: "AES-256-CBC", "AES-256-CFB", "AES-256-ECB", "AES-256-OFB",
064: "BF-CBC", "BF-CFB", "BF-ECB", "BF-OFB", "DES-CBC",
065: "DES-CFB", "DES-ECB", "DES-OFB", "DES-EDE", "DES-EDE-CBC",
066: "DES-EDE-CFB", "DES-EDE-ECB", "DES-EDE-OFB", "DES-EDE3",
067: "DES-EDE3-CBC", "DES-EDE3-CFB", "DES-EDE3-ECB",
068: "DES-EDE3-OFB", "RC2-CBC", "RC2-CFB", "RC2-ECB", "RC2-OFB",
069: "RC2-40-CBC", "RC2-64-CBC", };
070:
071: private class Password implements PasswordFinder {
072: private final char[] password;
073:
074: public Password(char[] word) {
075: this .password = (char[]) word.clone();
076: }
077:
078: public char[] getPassword() {
079: return (char[]) password.clone();
080: }
081: }
082:
083: public String getName() {
084: return "PEMWriterTest";
085: }
086:
087: public void performTest() throws Exception {
088: final String provider = "BC";
089:
090: KeyPairGenerator dsaKpg = KeyPairGenerator.getInstance("DSA",
091: provider);
092: dsaKpg.initialize(testDsaParams, random);
093:
094: KeyPair dsaKp = dsaKpg.generateKeyPair();
095: PrivateKey testDsaKey = dsaKp.getPrivate();
096:
097: doWriteReadTests(testDsaKey, provider, algorithms);
098:
099: KeyFactory fact = KeyFactory.getInstance("RSA", provider);
100: PrivateKey testRsaKey = fact.generatePrivate(testRsaKeySpec);
101:
102: doWriteReadTests(testRsaKey, provider, algorithms);
103: }
104:
105: private void doWriteReadTests(PrivateKey akp, String provider,
106: String[] algorithms) throws IOException {
107: for (int i = 0; i < algorithms.length; ++i) {
108: doWriteReadTest(akp, provider, algorithms[i]);
109: }
110: }
111:
112: private void doWriteReadTest(PrivateKey akp, String provider,
113: String algorithm) throws IOException {
114: StringWriter sw = new StringWriter();
115: PEMWriter pw = new PEMWriter(sw, provider);
116:
117: pw.writeObject(akp, algorithm, testPassword, random);
118: pw.close();
119:
120: String data = sw.toString();
121:
122: PEMReader pr = new PEMReader(new StringReader(data),
123: new Password(testPassword), provider);
124:
125: Object o = pr.readObject();
126:
127: if (o == null || !(o instanceof KeyPair)) {
128: fail("Didn't find OpenSSL key");
129: }
130:
131: KeyPair kp = (KeyPair) o;
132: PrivateKey privKey = kp.getPrivate();
133:
134: if (!akp.equals(privKey)) {
135: fail("Failed to read back test key encoded with: "
136: + algorithm);
137: }
138: }
139:
140: public static void main(String[] args) {
141: Security.addProvider(new BouncyCastleProvider());
142:
143: runTest(new WriterTest());
144: }
145: }
|