001: /*
002: * @(#)CertificateX509Key.java 1.17 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.security.PublicKey;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.io.OutputStream;
034: import java.util.Enumeration;
035:
036: import sun.security.util.*;
037:
038: /**
039: * This class defines the X509Key attribute for the Certificate.
040: *
041: * @author Amit Kapoor
042: * @author Hemma Prafullchandra
043: * @version 1.10
044: * @see CertAttrSet
045: */
046: public class CertificateX509Key implements CertAttrSet {
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.key";
052: /**
053: * Sub attributes name for this CertAttrSet.
054: */
055: public static final String NAME = "key";
056: public static final String KEY = "value";
057:
058: // Private data member
059: private PublicKey key;
060:
061: /**
062: * Default constructor for the certificate attribute.
063: *
064: * @param key the X509Key
065: */
066: public CertificateX509Key(PublicKey key) {
067: this .key = key;
068: }
069:
070: /**
071: * Create the object, decoding the values from the passed DER stream.
072: *
073: * @param in the DerInputStream to read the X509Key from.
074: * @exception IOException on decoding errors.
075: */
076: public CertificateX509Key(DerInputStream in) throws IOException {
077: DerValue val = in.getDerValue();
078: key = X509Key.parse(val);
079: }
080:
081: /**
082: * Create the object, decoding the values from the passed stream.
083: *
084: * @param in the InputStream to read the X509Key from.
085: * @exception IOException on decoding errors.
086: */
087: public CertificateX509Key(InputStream in) throws IOException {
088: DerValue val = new DerValue(in);
089: key = X509Key.parse(val);
090: }
091:
092: /**
093: * Return the key as printable string.
094: */
095: public String toString() {
096: if (key == null)
097: return "";
098: return (key.toString());
099: }
100:
101: /**
102: * Decode the key in DER form from the stream.
103: *
104: * @param in the InputStream to unmarshal the contents from
105: * @exception IOException on decoding or validity errors.
106: */
107: public void decode(InputStream in) throws IOException {
108: DerValue val = new DerValue(in);
109: key = X509Key.parse(val);
110: }
111:
112: /**
113: * Encode the key in DER form to the stream.
114: *
115: * @param out the OutputStream to marshal the contents to.
116: * @exception IOException on errors.
117: */
118: public void encode(OutputStream out) throws IOException {
119: DerOutputStream tmp = new DerOutputStream();
120: tmp.write(key.getEncoded());
121:
122: out.write(tmp.toByteArray());
123: }
124:
125: /**
126: * Set the attribute value.
127: */
128: public void set(String name, Object obj) throws IOException {
129: if (name.equalsIgnoreCase(KEY)) {
130: this .key = (PublicKey) obj;
131: } else {
132: throw new IOException("Attribute name not recognized by "
133: + "CertAttrSet: CertificateX509Key.");
134: }
135: }
136:
137: /**
138: * Get the attribute value.
139: */
140: public Object get(String name) throws IOException {
141: if (name.equalsIgnoreCase(KEY)) {
142: return (key);
143: } else {
144: throw new IOException("Attribute name not recognized by "
145: + "CertAttrSet: CertificateX509Key.");
146: }
147: }
148:
149: /**
150: * Delete the attribute value.
151: */
152: public void delete(String name) throws IOException {
153: if (name.equalsIgnoreCase(KEY)) {
154: key = null;
155: } else {
156: throw new IOException("Attribute name not recognized by "
157: + "CertAttrSet: CertificateX509Key.");
158: }
159: }
160:
161: /**
162: * Return an enumeration of names of attributes existing within this
163: * attribute.
164: */
165: public Enumeration getElements() {
166: AttributeNameEnumeration elements = new AttributeNameEnumeration();
167: elements.addElement(KEY);
168:
169: return (elements.elements());
170: }
171:
172: /**
173: * Return the name of this attribute.
174: */
175: public String getName() {
176: return (NAME);
177: }
178: }
|