001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.security.tests.x509.tsp;
019:
020: import java.io.IOException;
021: import java.math.BigInteger;
022: import java.util.Arrays;
023: import java.util.Collections;
024: import java.util.Date;
025:
026: import junit.framework.TestCase;
027:
028: import org.apache.harmony.security.asn1.ASN1Integer;
029: import org.apache.harmony.security.asn1.ASN1OctetString;
030: import org.apache.harmony.security.pkcs7.ContentInfo;
031: import org.apache.harmony.security.pkcs7.SignedData;
032: import org.apache.harmony.security.pkcs7.SignerInfo;
033: import org.apache.harmony.security.x501.Name;
034: import org.apache.harmony.security.x509.AlgorithmIdentifier;
035: import org.apache.harmony.security.x509.Extension;
036: import org.apache.harmony.security.x509.Extensions;
037: import org.apache.harmony.security.x509.GeneralName;
038: import org.apache.harmony.security.x509.tsp.MessageImprint;
039: import org.apache.harmony.security.x509.tsp.PKIFailureInfo;
040: import org.apache.harmony.security.x509.tsp.PKIStatus;
041: import org.apache.harmony.security.x509.tsp.PKIStatusInfo;
042: import org.apache.harmony.security.x509.tsp.TSTInfo;
043: import org.apache.harmony.security.x509.tsp.TimeStampResp;
044:
045: public class TimeStampRespTest extends TestCase {
046:
047: /**
048: * @throws IOException
049: * @tests 'org.apache.harmony.security.x509.tsp.TimeStampResp.getEncoded()'
050: */
051: public void testGetEncoded() throws IOException {
052: String statusString = "statusString";
053: PKIStatusInfo status = new PKIStatusInfo(PKIStatus.REJECTION,
054: Collections.singletonList(statusString),
055: PKIFailureInfo.BAD_REQUEST);
056:
057: // creating TimeStampToken
058: String policy = "1.2.3.4.5";
059: String sha1 = "1.3.14.3.2.26";
060: MessageImprint msgImprint = new MessageImprint(
061: new AlgorithmIdentifier(sha1), new byte[20]);
062: Date genTime = new Date();
063: BigInteger nonce = BigInteger.valueOf(1234567890L);
064: // accuracy is 1 second
065: int[] accuracy = new int[] { 1, 0, 0 };
066: GeneralName tsa = new GeneralName(new Name("CN=AnAuthority"));
067: Extensions exts = new Extensions();
068: // Time-Stamping extension OID: as defined in RFC 3161
069: int[] timeStampingExtOID = new int[] { 1, 3, 6, 1, 5, 5, 7, 3,
070: 8 };
071: byte[] timeStampingExtValue = new byte[] { (byte) 1, (byte) 2,
072: (byte) 3 };
073: Extension ext = new Extension(timeStampingExtOID, true,
074: timeStampingExtValue);
075: exts.addExtension(ext);
076:
077: TSTInfo tSTInfo = new TSTInfo(1, policy, msgImprint,
078: BigInteger.TEN, genTime, accuracy, Boolean.FALSE,
079: nonce, tsa, exts);
080:
081: Object[] issuerAndSerialNumber = new Object[] {
082: new Name("CN=issuer"), ASN1Integer.fromIntValue(12345) };
083: // SHA1withDSA OID
084: String sha1dsa = "1.2.840.10040.4.3";
085: SignerInfo sigInfo = new SignerInfo(1, issuerAndSerialNumber,
086: new AlgorithmIdentifier(sha1), null,
087: new AlgorithmIdentifier(sha1dsa), new byte[20], null);
088: // TSTInfo OID according to RFC 3161
089: int[] tSTInfoOid = new int[] { 1, 2, 840, 113549, 1, 9, 16, 1,
090: 4 };
091: ContentInfo tSTInfoEncoded = new ContentInfo(tSTInfoOid,
092: ASN1OctetString.getInstance().encode(
093: TSTInfo.ASN1.encode(tSTInfo)));
094: SignedData tokenContent = new SignedData(1, Collections
095: .singletonList(new AlgorithmIdentifier(sha1)),
096: tSTInfoEncoded, null, null, Collections
097: .singletonList(sigInfo));
098: ContentInfo timeStampToken = new ContentInfo(
099: ContentInfo.SIGNED_DATA, tokenContent);
100:
101: TimeStampResp response = new TimeStampResp(status,
102: timeStampToken);
103:
104: byte[] encoding = TimeStampResp.ASN1.encode(response);
105: TimeStampResp decoded = (TimeStampResp) TimeStampResp.ASN1
106: .decode(encoding);
107:
108: // deeper checks are performed in the corresponding unit tests
109: assertTrue("Decoded status is incorrect", Arrays.equals(
110: PKIStatusInfo.ASN1.encode(status), PKIStatusInfo.ASN1
111: .encode(decoded.getStatus())));
112: assertTrue("Decoded timeStampToken is incorrect", Arrays
113: .equals(timeStampToken.getEncoded(), decoded
114: .getTimeStampToken().getEncoded()));
115: }
116: }
|