OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / java / security / cert / PolicyQualifierInfo.java
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
18 package java.security.cert;
19
20 import java.io.IOException;
21 import org.apache.harmony.security.asn1.ObjectIdentifier;
22 import org.apache.harmony.security.utils.Array;
23
24
25 /**
26  * This class implements a policy qualifier as defined by the ASN.1
27  * {@code PolicyQualifierInfo} structure.
28  */
29 public class PolicyQualifierInfo {
30     // This PolicyQualifierInfo DER encoding
31     private final byte[] encoded;
32     // This PolicyQualifierInfo policy qualifier id -
33     // OID represented as String containing non-negative integers
34     // separated by periods
35     private final String policyQualifierId;
36     // DER encoding of the policy qualifier - part of encoded
37     private final byte[] policyQualifier;
38
39     /**
40      * Creates a new {@code PolicyQualifierInfo} from the specified encoded
41      * form.
42      *
43      * @param encoded
44      *            the DER encoded policy qualifier.
45      * @throws IOException
46      *             the policy qualifier cannot be decoded.
47      */
48     public PolicyQualifierInfo(byte[] encoded) throws IOException {
49         if (encoded == null) {
50             throw new NullPointerException("encoded == null");
51         }
52         if (encoded.length == 0) {
53             throw new IOException("encoded.length == 0");
54         }
55         this.encoded = new byte[encoded.length];
56         System.arraycopy(encoded, 0, this.encoded, 0, this.encoded.length);
57
58         // DER Decoding:
59         Object[] decoded = (Object[]) org.apache.harmony.security.x509.PolicyQualifierInfo.ASN1
60                 .decode(this.encoded);
61         policyQualifierId = ObjectIdentifier.toString((int[]) decoded[0]);
62         policyQualifier = (byte[]) decoded[1];
63     }
64
65     /**
66      * Returns a ASN.1 DER encoded copy of policy qualifier info.
67      *
68      * @return a ASN.1 DER encoded copy of policy qualifier info.
69      */
70     public final byte[] getEncoded() {
71         byte[] ret = new byte[encoded.length];
72         System.arraycopy(encoded, 0, ret, 0, encoded.length);
73         return ret;
74     }
75
76     /**
77      * Returns the identifier (an OID) of this policy qualifier info.
78      *
79      * @return the identifier of this policy qualifier info.
80      */
81     public final String getPolicyQualifierId() {
82         return policyQualifierId;
83     }
84
85     /**
86      * Returns a ASN.1 DER encoded copy of the qualifier of this policy
87      * qualifier info.
88      *
89      * @return a ASN.1 DER encoded copy of the qualifier of this policy
90      *         qualifier info.
91      */
92     public final byte[] getPolicyQualifier() {
93         if (policyQualifier == null) {
94             return null;
95         }
96         byte[] ret = new byte[policyQualifier.length];
97         System.arraycopy(policyQualifier, 0, ret, 0, policyQualifier.length);
98         return ret;
99     }
100
101     /**
102      * Returns a string representation of this {@code PolicyQualifierInfo}
103      * instance.
104      *
105      * @return a string representation of this {@code PolicyQualifierInfo}
106      *         instance.
107      */
108     public String toString() {
109         StringBuilder sb =
110             new StringBuilder("PolicyQualifierInfo: [\npolicyQualifierId: ");
111         sb.append(policyQualifierId);
112         sb.append("\npolicyQualifier: \n");
113         sb.append(Array.toString(policyQualifier, " "));
114         sb.append("]");
115         return sb.toString();
116     }
117 }