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