1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.security.tests.x509.tsp;
19
20import java.io.IOException;
21import java.math.BigInteger;
22import java.util.Arrays;
23import java.util.Collections;
24import java.util.Date;
25
26import junit.framework.TestCase;
27
28import org.apache.harmony.security.asn1.ASN1Integer;
29import org.apache.harmony.security.asn1.ASN1OctetString;
30import org.apache.harmony.security.pkcs7.ContentInfo;
31import org.apache.harmony.security.pkcs7.SignedData;
32import org.apache.harmony.security.pkcs7.SignerInfo;
33import org.apache.harmony.security.x501.Name;
34import org.apache.harmony.security.x509.AlgorithmIdentifier;
35import org.apache.harmony.security.x509.Extension;
36import org.apache.harmony.security.x509.Extensions;
37import org.apache.harmony.security.x509.GeneralName;
38import org.apache.harmony.security.x509.tsp.MessageImprint;
39import org.apache.harmony.security.x509.tsp.PKIFailureInfo;
40import org.apache.harmony.security.x509.tsp.PKIStatus;
41import org.apache.harmony.security.x509.tsp.PKIStatusInfo;
42import org.apache.harmony.security.x509.tsp.TSTInfo;
43import org.apache.harmony.security.x509.tsp.TimeStampResp;
44
45public class TimeStampRespTest extends TestCase {
46
47    /**
48     * @throws IOException
49     * @tests 'org.apache.harmony.security.x509.tsp.TimeStampResp.getEncoded()'
50     */
51    public void testGetEncoded() throws IOException {
52        String statusString = "statusString";
53        PKIStatusInfo status = new PKIStatusInfo(PKIStatus.REJECTION,
54                Collections.singletonList(statusString),
55                PKIFailureInfo.BAD_REQUEST);
56
57
58        // creating TimeStampToken
59        String policy = "1.2.3.4.5";
60        String sha1 = "1.3.14.3.2.26";
61        MessageImprint msgImprint = new MessageImprint(new AlgorithmIdentifier(
62                sha1), new byte[20]);
63        Date genTime = new Date();
64        BigInteger nonce = BigInteger.valueOf(1234567890L);
65        // accuracy is 1 second
66        int[] accuracy = new int[] { 1, 0, 0 };
67        GeneralName tsa = new GeneralName(new Name("CN=AnAuthority"));
68        Extensions exts = new Extensions();
69        // Time-Stamping extension OID: as defined in RFC 3161
70        int[] timeStampingExtOID = new int[] { 1, 3, 6, 1, 5, 5, 7, 3, 8 };
71        byte[] timeStampingExtValue = new byte[] { (byte) 1, (byte) 2, (byte) 3 };
72        Extension ext = new Extension(timeStampingExtOID, true,
73                timeStampingExtValue);
74        exts.addExtension(ext);
75
76        TSTInfo tSTInfo = new TSTInfo(1, policy, msgImprint, BigInteger.TEN,
77                genTime, accuracy, Boolean.FALSE, nonce, tsa, exts);
78
79        Object[] issuerAndSerialNumber = new Object[] { new Name("CN=issuer"),
80                ASN1Integer.fromIntValue(12345) };
81        // SHA1withDSA OID
82        String sha1dsa = "1.2.840.10040.4.3";
83        SignerInfo sigInfo = new SignerInfo(1, issuerAndSerialNumber,
84                new AlgorithmIdentifier(sha1), null, new AlgorithmIdentifier(
85                sha1dsa), new byte[20], null);
86        // TSTInfo OID according to RFC 3161
87        int[] tSTInfoOid = new int[] { 1, 2, 840, 113549, 1, 9, 16, 1, 4 };
88        ContentInfo tSTInfoEncoded = new ContentInfo(tSTInfoOid,
89                ASN1OctetString.getInstance().encode(
90                        TSTInfo.ASN1.encode(tSTInfo)));
91        SignedData tokenContent = new SignedData(1, Collections
92                .singletonList(new AlgorithmIdentifier(sha1)), tSTInfoEncoded,
93                null, null, Collections.singletonList(sigInfo));
94        ContentInfo timeStampToken = new ContentInfo(ContentInfo.SIGNED_DATA,
95                tokenContent);
96
97        TimeStampResp response = new TimeStampResp(status, timeStampToken);
98
99        byte[] encoding = TimeStampResp.ASN1.encode(response);
100        TimeStampResp decoded = (TimeStampResp) TimeStampResp.ASN1
101                .decode(encoding);
102
103        // deeper checks are performed in the corresponding unit tests
104        assertTrue("Decoded status is incorrect", Arrays.equals(
105                PKIStatusInfo.ASN1.encode(status), PKIStatusInfo.ASN1
106                .encode(decoded.getStatus())));
107        assertTrue("Decoded timeStampToken is incorrect", Arrays.equals(
108                timeStampToken.getEncoded(), decoded.getTimeStampToken()
109                .getEncoded()));
110    }
111}
112
113