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/**
19* @author Alexander Y. Kleymenov
20* @version $Revision$
21*/
22
23package org.apache.harmony.security.x509;
24
25import java.util.Arrays;
26
27import org.apache.harmony.security.asn1.ASN1Any;
28import org.apache.harmony.security.asn1.ASN1Oid;
29import org.apache.harmony.security.asn1.ASN1Sequence;
30import org.apache.harmony.security.asn1.ASN1Type;
31import org.apache.harmony.security.asn1.BerInputStream;
32import org.apache.harmony.security.asn1.ObjectIdentifier;
33import org.apache.harmony.security.utils.AlgNameMapper;
34
35
36/**
37 * The class encapsulates the ASN.1 DER encoding/decoding work
38 * with the Algorithm Identifier which is a part of X.509 certificate
39 * (as specified in RFC 3280 -
40 *  Internet X.509 Public Key Infrastructure.
41 *  Certificate and Certificate Revocation List (CRL) Profile.
42 *  http://www.ietf.org/rfc/rfc3280.txt):
43 *
44 * <pre>
45 *  AlgorithmIdentifier ::= SEQUENCE {
46 *      algorithm OBJECT IDENTIFIER,
47 *      parameters ANY DEFINED BY algorithm OPTIONAL
48 *  }
49 * </pre>
50 */
51public class AlgorithmIdentifier {
52
53    // the value of algorithm field
54    private String algorithm;
55    // the name of the algorithm
56    private String algorithmName;
57    // the value of parameters field
58    private byte[] parameters;
59    // the encoding of AlgorithmIdentifier value
60    private byte[] encoding;
61
62    /**
63     * TODO
64     * @param   algorithm:  String
65     */
66    public AlgorithmIdentifier(String algorithm) {
67        this(algorithm, null, null);
68    }
69
70    /**
71     * TODO
72     * @param   algorithm:  String
73     * @param   parameters: byte[]
74     */
75    public AlgorithmIdentifier(String algorithm, byte[] parameters) {
76        this(algorithm, parameters, null);
77    }
78
79    //
80    // TODO
81    // @param   algorithm:  String
82    // @param   parameters: byte[]
83    // @param   encoding:   byte[]
84    //
85    private AlgorithmIdentifier(String algorithm, byte[] parameters,
86                                byte[] encoding) {
87        this.algorithm = algorithm;
88        this.parameters = parameters;
89        this.encoding = encoding;
90    }
91
92    /**
93     * Returns the value of algorithm field of the structure.
94     * @return  algorithm
95     */
96    public String getAlgorithm() {
97        return algorithm;
98    }
99
100    /**
101     * Returns the name of the algorithm corresponding to
102     * its OID. If there is no the such correspondence,
103     * algorithm OID is returned.
104     * @return  algorithm
105     */
106    public String getAlgorithmName() {
107        if (algorithmName == null) {
108            algorithmName = AlgNameMapper.map2AlgName(algorithm);
109            if (algorithmName == null) {
110                algorithmName = algorithm;
111            }
112        }
113        return algorithmName;
114    }
115
116    /**
117     * Returns the value of parameters field of the structure.
118     * @return  parameters
119     */
120    public byte[] getParameters() {
121        return parameters;
122    }
123
124    /**
125     * Returns ASN.1 encoded form of this X.509 AlgorithmIdentifier value.
126     * @return a byte array containing ASN.1 encode form.
127     */
128    public byte[] getEncoded() {
129        if (encoding == null) {
130            encoding = ASN1.encode(this);
131        }
132        return encoding;
133    }
134
135    public boolean equals(Object ai) {
136        if (!(ai instanceof AlgorithmIdentifier)) {
137            return false;
138        }
139        AlgorithmIdentifier algid = (AlgorithmIdentifier) ai;
140        return (algorithm.equals(algid.algorithm))
141            && ((parameters == null)
142                    ? algid.parameters == null
143                    : Arrays.equals(parameters, algid.parameters));
144    }
145
146    public int hashCode() {
147    	return algorithm.hashCode() * 37 +
148    		(parameters != null ? parameters.hashCode() : 0);
149    }
150
151    /**
152     * Places the string representation into the StringBuffer object.
153     */
154    public void dumpValue(StringBuffer buffer) {
155        buffer.append(getAlgorithmName());
156        if (parameters == null) {
157            buffer.append(", no params, "); //$NON-NLS-1$
158        } else {
159            buffer.append(", params unparsed, "); //$NON-NLS-1$
160        }
161        buffer.append("OID = "); //$NON-NLS-1$
162        buffer.append(getAlgorithm());
163    }
164
165    /**
166     * Custom AlgorithmIdentifier DER encoder/decoder
167     */
168    public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
169            ASN1Oid.getInstance(), ASN1Any.getInstance() }) {
170        {
171            setOptional(1); // parameters are optional
172        }
173
174        protected Object getDecodedObject(BerInputStream in) {
175            Object[] values = (Object[]) in.content;
176            return new AlgorithmIdentifier(ObjectIdentifier
177                    .toString((int[]) values[0]), (byte[]) values[1]);
178        }
179
180        protected void getValues(Object object, Object[] values) {
181
182            AlgorithmIdentifier aID = (AlgorithmIdentifier) object;
183
184            values[0] = ObjectIdentifier.toIntArray(aID.getAlgorithm());
185            values[1] = aID.getParameters();
186        }
187    };
188
189}
190
191