View Javadoc
1   /*
2    * Copyright (c) 1997, 2011, Oracle and/or its affiliates. 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.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package sun.security.x509;
27  
28  import java.security.PublicKey;
29  import java.io.InputStream;
30  import java.io.IOException;
31  import java.io.OutputStream;
32  import java.util.Enumeration;
33  
34  import 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   */
43  public 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 PublicKey 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 }