1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20package org.apache.harmony.security.x509.tsp;
21
22import java.math.BigInteger;
23import java.util.List;
24import org.apache.harmony.security.asn1.ASN1BitString;
25import org.apache.harmony.security.asn1.ASN1Integer;
26import org.apache.harmony.security.asn1.ASN1Sequence;
27import org.apache.harmony.security.asn1.ASN1SequenceOf;
28import org.apache.harmony.security.asn1.ASN1StringType;
29import org.apache.harmony.security.asn1.ASN1Type;
30import org.apache.harmony.security.asn1.BerInputStream;
31import org.apache.harmony.security.asn1.BitString;
32
33
34/**
35 * As defined in Time-Stamp Protocol (TSP)
36 * (http://www.ietf.org/rfc/rfc3161.txt)
37 *
38 * PKIStatusInfo ::= SEQUENCE {
39 *    status PKIStatus,
40 *    statusString PKIFreeText OPTIONAL,
41 *    failInfo PKIFailureInfo OPTIONAL
42 * }
43 *
44 */
45public class PKIStatusInfo {
46
47    private final PKIStatus status;
48
49    private final List statusString;
50
51    private final PKIFailureInfo failInfo;
52
53    public PKIStatusInfo(PKIStatus pKIStatus, List statusString,
54            PKIFailureInfo failInfo) {
55        this.status = pKIStatus;
56        this.statusString = statusString;
57        this.failInfo = failInfo;
58    }
59
60    public String toString(){
61        StringBuilder res = new StringBuilder();
62        res.append("-- PKIStatusInfo:");
63        res.append("\nPKIStatus : ");
64        res.append(status);
65        res.append("\nstatusString:  ");
66        res.append(statusString);
67        res.append("\nfailInfo:  ");
68        res.append(failInfo);
69        res.append("\n-- PKIStatusInfo End\n");
70        return res.toString();
71    }
72
73    /**
74     * @return Returns the failInfo.
75     */
76    public PKIFailureInfo getFailInfo() {
77        return failInfo;
78    }
79
80    /**
81     * @return Returns the pKIStatus.
82     */
83    public PKIStatus getStatus() {
84        return status;
85    }
86
87    /**
88     * @return Returns the statusString.
89     */
90    public List getStatusString() {
91        return statusString;
92    }
93
94    public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
95        ASN1Integer.getInstance(),                      // status
96        new ASN1SequenceOf(ASN1StringType.UTF8STRING),  // statusString
97        ASN1BitString.getInstance() }) {                // failInfo
98        {
99            setOptional(1);
100            setOptional(2);
101        }
102
103        protected void getValues(Object object, Object[] values) {
104            PKIStatusInfo psi = (PKIStatusInfo) object;
105            values[0] = BigInteger.valueOf(psi.status.getStatus())
106                    .toByteArray();
107            values[1] = psi.statusString;
108            if (psi.failInfo != null) {
109                // set the needed bit in the bit string
110                boolean[] failInfoBoolArray = new boolean[PKIFailureInfo
111                        .getMaxValue()];
112                failInfoBoolArray[psi.failInfo.getValue()] = true;
113                values[2] = new BitString(failInfoBoolArray);
114            } else {
115                values[2] = null;
116            }
117        }
118
119        protected Object getDecodedObject(BerInputStream in) {
120            Object[] values = (Object[]) in.content;
121
122            int failInfoValue = -1;
123            if (values[2] != null) {
124                boolean[] failInfoBoolArray = ((BitString) values[2]).toBooleanArray();
125                for (int i = 0; i < failInfoBoolArray.length; i++) {
126                    if (failInfoBoolArray[i]) {
127                        failInfoValue = i;
128                        break;
129                    }
130                }
131            }
132            return new PKIStatusInfo(
133                    PKIStatus.getInstance(ASN1Integer.toIntValue(values[0])),
134                    (List)values[1],
135                    PKIFailureInfo.getInstance(failInfoValue));
136        }
137    };
138}
139
140