001: package org.bouncycastle.asn1.test;
002:
003: import org.bouncycastle.asn1.ASN1InputStream;
004: import org.bouncycastle.asn1.DERGeneralizedTime;
005: import org.bouncycastle.asn1.DERObject;
006: import org.bouncycastle.asn1.DEROctetString;
007: import org.bouncycastle.asn1.cms.RecipientKeyIdentifier;
008: import org.bouncycastle.asn1.smime.SMIMECapabilitiesAttribute;
009: import org.bouncycastle.asn1.smime.SMIMECapability;
010: import org.bouncycastle.asn1.smime.SMIMECapabilityVector;
011: import org.bouncycastle.asn1.smime.SMIMEEncryptionKeyPreferenceAttribute;
012: import org.bouncycastle.util.encoders.Base64;
013: import org.bouncycastle.util.test.SimpleTestResult;
014: import org.bouncycastle.util.test.Test;
015: import org.bouncycastle.util.test.TestResult;
016:
017: import java.io.ByteArrayInputStream;
018:
019: public class SMIMETest implements Test {
020: byte[] attrBytes = Base64
021: .decode("MDQGCSqGSIb3DQEJDzEnMCUwCgYIKoZIhvcNAwcwDgYIKoZIhvcNAwICAgCAMAcGBSsOAwIH");
022: byte[] prefBytes = Base64
023: .decode("MCwGCyqGSIb3DQEJEAILMR2hGwQIAAAAAAAAAAAYDzIwMDcwMzE1MTczNzI5Wg==");
024:
025: private boolean isSameAs(byte[] a, byte[] b) {
026: if (a.length != b.length) {
027: return false;
028: }
029:
030: for (int i = 0; i != a.length; i++) {
031: if (a[i] != b[i]) {
032: return false;
033: }
034: }
035:
036: return true;
037: }
038:
039: public TestResult perform() {
040: SMIMECapabilityVector caps = new SMIMECapabilityVector();
041:
042: caps.addCapability(SMIMECapability.dES_EDE3_CBC);
043: caps.addCapability(SMIMECapability.rC2_CBC, 128);
044: caps.addCapability(SMIMECapability.dES_CBC);
045:
046: SMIMECapabilitiesAttribute attr = new SMIMECapabilitiesAttribute(
047: caps);
048:
049: SMIMEEncryptionKeyPreferenceAttribute pref = new SMIMEEncryptionKeyPreferenceAttribute(
050: new RecipientKeyIdentifier(new DEROctetString(
051: new byte[8]), new DERGeneralizedTime(
052: "20070315173729Z"), null));
053:
054: try {
055: if (!isSameAs(attr.getEncoded(), attrBytes)) {
056: return new SimpleTestResult(false, getName()
057: + ": Failed attr data check");
058: }
059:
060: ByteArrayInputStream bIn = new ByteArrayInputStream(
061: attrBytes);
062: ASN1InputStream aIn = new ASN1InputStream(bIn);
063:
064: DERObject o = aIn.readObject();
065: if (!attr.equals(o)) {
066: return new SimpleTestResult(false, getName()
067: + ": Failed equality test for attr");
068: }
069:
070: if (!isSameAs(pref.getEncoded(), prefBytes)) {
071: return new SimpleTestResult(false, getName()
072: + ": Failed attr data check");
073: }
074:
075: bIn = new ByteArrayInputStream(prefBytes);
076: aIn = new ASN1InputStream(bIn);
077:
078: o = aIn.readObject();
079: if (!pref.equals(o)) {
080: return new SimpleTestResult(false, getName()
081: + ": Failed equality test for pref");
082: }
083:
084: return new SimpleTestResult(true, getName() + ": Okay");
085: } catch (Exception e) {
086: return new SimpleTestResult(false, getName()
087: + ": Failed - exception " + e.toString(), e);
088: }
089: }
090:
091: public String getName() {
092: return "SMIME";
093: }
094:
095: public static void main(String[] args) {
096: SMIMETest test = new SMIMETest();
097: TestResult result = test.perform();
098:
099: System.out.println(result);
100: }
101: }
|