001: /*
002: * @(#)CertificateAlgorithmId.java 1.19 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.security.x509;
029:
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.util.Enumeration;
034:
035: import sun.security.util.*;
036:
037: /**
038: * This class defines the AlgorithmId for the Certificate.
039: *
040: * @author Amit Kapoor
041: * @author Hemma Prafullchandra
042: * @version 1.12
043: */
044: public class CertificateAlgorithmId implements CertAttrSet {
045: private AlgorithmId algId;
046:
047: /**
048: * Identifier for this attribute, to be used with the
049: * get, set, delete methods of Certificate, x509 type.
050: */
051: public static final String IDENT = "x509.info.algorithmID";
052: /**
053: * Sub attributes name for this CertAttrSet.
054: */
055: public static final String NAME = "algorithmID";
056:
057: /**
058: * Identifier to be used with get, set, and delete methods. When
059: * using this identifier the associated object being passed in or
060: * returned is an instance of AlgorithmId.
061: * @see sun.security.x509.AlgorithmId
062: */
063: public static final String ALGORITHM = "algorithm";
064:
065: /**
066: * Default constructor for the certificate attribute.
067: *
068: * @param algId the Algorithm identifier
069: */
070: public CertificateAlgorithmId(AlgorithmId algId) {
071: this .algId = algId;
072: }
073:
074: /**
075: * Create the object, decoding the values from the passed DER stream.
076: *
077: * @param in the DerInputStream to read the serial number from.
078: * @exception IOException on decoding errors.
079: */
080: public CertificateAlgorithmId(DerInputStream in) throws IOException {
081: DerValue val = in.getDerValue();
082: algId = AlgorithmId.parse(val);
083: }
084:
085: /**
086: * Create the object, decoding the values from the passed stream.
087: *
088: * @param in the InputStream to read the serial number from.
089: * @exception IOException on decoding errors.
090: */
091: public CertificateAlgorithmId(InputStream in) throws IOException {
092: DerValue val = new DerValue(in);
093: algId = AlgorithmId.parse(val);
094: }
095:
096: /**
097: * Return the algorithm identifier as user readable string.
098: */
099: public String toString() {
100: if (algId == null)
101: return "";
102: return (algId.toString() + ", OID = "
103: + (algId.getOID()).toString() + "\n");
104: }
105:
106: /**
107: * Encode the algorithm identifier in DER form to the stream.
108: *
109: * @param out the DerOutputStream to marshal the contents to.
110: * @exception IOException on errors.
111: */
112: public void encode(OutputStream out) throws IOException {
113: DerOutputStream tmp = new DerOutputStream();
114: algId.encode(tmp);
115:
116: out.write(tmp.toByteArray());
117: }
118:
119: /**
120: * Decode the algorithm identifier from the passed stream.
121: *
122: * @param in the InputStream to unmarshal the contents from.
123: * @exception IOException on errors.
124: */
125: public void decode(InputStream in) throws IOException {
126: DerValue derVal = new DerValue(in);
127: algId = AlgorithmId.parse(derVal);
128: }
129:
130: /**
131: * Set the attribute value.
132: */
133: public void set(String name, Object obj) throws IOException {
134: if (!(obj instanceof AlgorithmId)) {
135: throw new IOException(
136: "Attribute must be of type AlgorithmId.");
137: }
138: if (name.equalsIgnoreCase(ALGORITHM)) {
139: algId = (AlgorithmId) obj;
140: } else {
141: throw new IOException("Attribute name not recognized by "
142: + "CertAttrSet:CertificateAlgorithmId.");
143: }
144: }
145:
146: /**
147: * Get the attribute value.
148: */
149: public Object get(String name) throws IOException {
150: if (name.equalsIgnoreCase(ALGORITHM)) {
151: return (algId);
152: } else {
153: throw new IOException("Attribute name not recognized by "
154: + "CertAttrSet:CertificateAlgorithmId.");
155: }
156: }
157:
158: /**
159: * Delete the attribute value.
160: */
161: public void delete(String name) throws IOException {
162: if (name.equalsIgnoreCase(ALGORITHM)) {
163: algId = null;
164: } else {
165: throw new IOException("Attribute name not recognized by "
166: + "CertAttrSet:CertificateAlgorithmId.");
167: }
168: }
169:
170: /**
171: * Return an enumeration of names of attributes existing within this
172: * attribute.
173: */
174: public Enumeration getElements() {
175: AttributeNameEnumeration elements = new AttributeNameEnumeration();
176: elements.addElement(ALGORITHM);
177: return (elements.elements());
178: }
179:
180: /**
181: * Return the name of this attribute.
182: */
183: public String getName() {
184: return (NAME);
185: }
186: }
|