blob: bf8bd6697729af831c8d88e7956a6d96935a1f8d [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.security.x509;
27
28import java.security.PublicKey;
29import java.io.InputStream;
30import java.io.IOException;
31import java.io.OutputStream;
32import java.util.Enumeration;
33
34import sun.security.util.*;
35
36/**
37 * This class defines the X509Key attribute for the Certificate.
38 *
39 * @author Amit Kapoor
40 * @author Hemma Prafullchandra
41 * @see CertAttrSet
42 */
43public class CertificateX509Key implements CertAttrSet<String> {
44 /**
45 * Identifier for this attribute, to be used with the
46 * get, set, delete methods of Certificate, x509 type.
47 */
48 public static final String IDENT = "x509.info.key";
49 /**
50 * Sub attributes name for this CertAttrSet.
51 */
52 public static final String NAME = "key";
53 public static final String KEY = "value";
54
55 // Private data member
56 private PublicKey key;
57
58 /**
59 * Default constructor for the certificate attribute.
60 *
61 * @param key the X509Key
62 */
63 public CertificateX509Key(PublicKey key) {
64 this.key = key;
65 }
66
67 /**
68 * Create the object, decoding the values from the passed DER stream.
69 *
70 * @param in the DerInputStream to read the X509Key from.
71 * @exception IOException on decoding errors.
72 */
73 public CertificateX509Key(DerInputStream in) throws IOException {
74 DerValue val = in.getDerValue();
75 key = X509Key.parse(val);
76 }
77
78 /**
79 * Create the object, decoding the values from the passed stream.
80 *
81 * @param in the InputStream to read the X509Key from.
82 * @exception IOException on decoding errors.
83 */
84 public CertificateX509Key(InputStream in) throws IOException {
85 DerValue val = new DerValue(in);
86 key = X509Key.parse(val);
87 }
88
89 /**
90 * Return the key as printable string.
91 */
92 public String toString() {
93 if (key == null) return "";
94 return(key.toString());
95 }
96
97 /**
98 * Encode the key in DER form to the stream.
99 *
100 * @param out the OutputStream to marshal the contents to.
101 * @exception IOException on errors.
102 */
103 public void encode(OutputStream out) throws IOException {
104 DerOutputStream tmp = new DerOutputStream();
105 tmp.write(key.getEncoded());
106
107 out.write(tmp.toByteArray());
108 }
109
110 /**
111 * Set the attribute value.
112 */
113 public void set(String name, Object obj) throws IOException {
114 if (name.equalsIgnoreCase(KEY)) {
115 this.key = (PublicKey)obj;
116 } else {
117 throw new IOException("Attribute name not recognized by " +
118 "CertAttrSet: CertificateX509Key.");
119 }
120 }
121
122 /**
123 * Get the attribute value.
124 */
125 public Object get(String name) throws IOException {
126 if (name.equalsIgnoreCase(KEY)) {
127 return(key);
128 } else {
129 throw new IOException("Attribute name not recognized by " +
130 "CertAttrSet: CertificateX509Key.");
131 }
132 }
133
134 /**
135 * Delete the attribute value.
136 */
137 public void delete(String name) throws IOException {
138 if (name.equalsIgnoreCase(KEY)) {
139 key = null;
140 } else {
141 throw new IOException("Attribute name not recognized by " +
142 "CertAttrSet: CertificateX509Key.");
143 }
144 }
145
146 /**
147 * Return an enumeration of names of attributes existing within this
148 * attribute.
149 */
150 public Enumeration<String> getElements() {
151 AttributeNameEnumeration elements = new AttributeNameEnumeration();
152 elements.addElement(KEY);
153
154 return(elements.elements());
155 }
156
157 /**
158 * Return the name of this attribute.
159 */
160 public String getName() {
161 return(NAME);
162 }
163}