001: package org.bouncycastle.asn1.test;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005:
006: import org.bouncycastle.asn1.ASN1InputStream;
007: import org.bouncycastle.asn1.ASN1Sequence;
008: import org.bouncycastle.asn1.DERObject;
009: import org.bouncycastle.asn1.DEROutputStream;
010: import org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo;
011: import org.bouncycastle.asn1.util.ASN1Dump;
012: import org.bouncycastle.util.encoders.Base64;
013: import org.bouncycastle.util.test.SimpleTest;
014:
015: /**
016: * Test the reading and writing of EncryptedPrivateKeyInfo objects using
017: * the test vectors provided at
018: * <a href=http://www.rsasecurity.com/rsalabs/pkcs/pkcs-5/index.html>
019: * RSA's PKCS5 Page</a>.
020: * <br>
021: * The vectors are Base 64 encoded and encrypted using the password "password"
022: * (without quotes). They should all yield the same PrivateKeyInfo object.
023: */
024: public class EncryptedPrivateKeyInfoTest extends SimpleTest {
025: static byte[] sample1 = Base64
026: .decode("MIIBozA9BgkqhkiG9w0BBQ0wMDAbBgkqhkiG9w0BBQwwDgQIfWBDXwLp4K4CAggA"
027: + "MBEGBSsOAwIHBAiaCF/AvOgQ6QSCAWDWX4BdAzCRNSQSANSuNsT5X8mWYO27mr3Y"
028: + "9c9LoBVXGNmYWKA77MI4967f7SmjNcgXj3xNE/jmnVz6hhsjS8E5VPT3kfyVkpdZ"
029: + "0lr5e9Yk2m3JWpPU7++v5zBkZmC4V/MwV/XuIs6U+vykgzMgpxQg0oZKS9zgmiZo"
030: + "f/4dOCL0UtCDnyOSvqT7mCVIcMDIEKu8QbVlgZYBop08l60EuEU3gARUo8WsYQmO"
031: + "Dz/ldx0Z+znIT0SXVuOwc+RVItC5T/Qx+aijmmpt+9l14nmaGBrEkmuhmtdvU/4v"
032: + "aptewGRgmjOfD6cqK+zs0O5NrrJ3P/6ZSxXj91CQgrThGfOv72bUncXEMNtc8pks"
033: + "2jpHFjGMdKufnadAD7XuMgzkkaklEXZ4f5tU6heIIwr51g0GBEGF96gYPFnjnSQM"
034: + "75JE02Clo+DfcfXpcybPTwwFg2jd6JTTOfkdf6OdSlA/1XNK43FA");
035:
036: static byte[] sample2 = Base64
037: .decode("MIIBpjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIeFeOWl1jywYCAggA"
038: + "MBQGCCqGSIb3DQMHBAjUJ5eGBhQGtQSCAWBrHrRgqO8UUMLcWzZEtpk1l3mjxiF/"
039: + "koCMkHsFwowgyWhEbgIkTgbSViK54LVK8PskekcGNLph+rB6bGZ7pPbL5pbXASJ8"
040: + "+MkQcG3FZdlS4Ek9tTJDApj3O1UubZGFG4uvTlJJFbF1BOJ3MkY3XQ9Gl1qwv7j5"
041: + "6e103Da7Cq9+oIDKmznza78XXQYrUsPo8mJGjUxPskEYlzwvHjKubRnYm/K6RKhi"
042: + "5f4zX4BQ/Dt3H812ZjRXrsjAJP0KrD/jyD/jCT7zNBVPH1izBds+RwizyQAHwfNJ"
043: + "BFR78TH4cgzB619X47FDVOnT0LqQNVd0O3cSwnPrXE9XR3tPayE+iOB15llFSmi8"
044: + "z0ByOXldEpkezCn92Umk++suzIVj1qfsK+bv2phZWJPbLEIWPDRHUbYf76q5ArAr"
045: + "u4xtxT/hoK3krEs/IN3d70qjlUJ36SEw1UaZ82PWhakQbdtu39ZraMJB");
046:
047: static byte[] sample3 = Base64
048: .decode("MIIBrjBIBgkqhkiG9w0BBQ0wOzAeBgkqhkiG9w0BBQwwEQQIrHyQPBZqWLUCAggA"
049: + "AgEQMBkGCCqGSIb3DQMCMA0CAToECEhbh7YZKiPSBIIBYCT1zp6o5jpFlIkgwPop"
050: + "7bW1+8ACr4exqzkeb3WflQ8cWJ4cURxzVdvxUnXeW1VJdaQZtjS/QHs5GhPTG/0f"
051: + "wtvnaPfwrIJ3FeGaZfcg2CrYhalOFmEb4xrE4KyoEQmUN8tb/Cg94uzd16BOPw21"
052: + "RDnE8bnPdIGY7TyL95kbkqH23mK53pi7h+xWIgduW+atIqDyyt55f7WMZcvDvlj6"
053: + "VpN/V0h+qxBHL274WA4dj6GYgeyUFpi60HdGCK7By2TBy8h1ZvKGjmB9h8jZvkx1"
054: + "MkbRumXxyFsowTZawyYvO8Um6lbfEDP9zIEUq0IV8RqH2MRyblsPNSikyYhxX/cz"
055: + "tdDxRKhilySbSBg5Kr8OfcwKp9bpinN96nmG4xr3Tch1bnVvqJzOQ5+Vva2WwVvH"
056: + "2JkWvYm5WaANg4Q6bRxu9vz7DuhbJjQdZbxFezIAgrJdSe92B00jO/0Kny1WjiVO"
057: + "6DA=");
058:
059: public String getName() {
060: return "EncryptedPrivateKeyInfoTest";
061: }
062:
063: private void test(int id, byte[] sample) {
064: ByteArrayInputStream bIn = new ByteArrayInputStream(sample);
065: ASN1InputStream aIn = new ASN1InputStream(bIn);
066: EncryptedPrivateKeyInfo info = null;
067:
068: try {
069: info = new EncryptedPrivateKeyInfo((ASN1Sequence) aIn
070: .readObject());
071: } catch (Exception e) {
072: fail("test " + id + " failed construction - exception "
073: + e.toString(), e);
074: }
075:
076: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
077: DEROutputStream dOut = new DEROutputStream(bOut);
078:
079: try {
080: dOut.writeObject(info);
081: } catch (Exception e) {
082: fail("test " + id + " failed writing - exception "
083: + e.toString(), e);
084: }
085:
086: byte[] bytes = bOut.toByteArray();
087:
088: if (bytes.length != sample.length) {
089: try {
090: bIn = new ByteArrayInputStream(bytes);
091: aIn = new ASN1InputStream(bIn);
092:
093: DERObject obj = aIn.readObject();
094:
095: fail("test " + id + " length mismatch - expected "
096: + sample.length
097: + System.getProperty("line.separator")
098: + ASN1Dump.dumpAsString(info) + " got "
099: + bytes.length
100: + System.getProperty("line.separator")
101: + ASN1Dump.dumpAsString(obj));
102: } catch (Exception e) {
103: fail("test " + id + " length mismatch - exception "
104: + e.toString());
105: }
106: }
107:
108: for (int i = 0; i != bytes.length; i++) {
109: if (bytes[i] != sample[i]) {
110: fail("test " + id + " data mismatch");
111: }
112: }
113: }
114:
115: public void performTest() {
116: test(0, sample1);
117: test(1, sample2);
118: test(2, sample3);
119: }
120:
121: public static void main(String[] args) {
122: runTest(new EncryptedPrivateKeyInfoTest());
123: }
124: }
|