Path: blob/master/src/java.base/share/classes/sun/security/x509/CertificateX509Key.java
41159 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.x509;2627import java.security.PublicKey;28import java.io.InputStream;29import java.io.IOException;30import java.io.OutputStream;31import java.util.Enumeration;3233import sun.security.util.*;3435/**36* This class defines the X509Key attribute for the Certificate.37*38* @author Amit Kapoor39* @author Hemma Prafullchandra40* @see CertAttrSet41*/42public class CertificateX509Key implements CertAttrSet<String> {43/**44* Identifier for this attribute, to be used with the45* get, set, delete methods of Certificate, x509 type.46*/47public static final String IDENT = "x509.info.key";48/**49* Sub attributes name for this CertAttrSet.50*/51public static final String NAME = "key";52public static final String KEY = "value";5354// Private data member55private PublicKey key;5657/**58* Default constructor for the certificate attribute.59*60* @param key the X509Key61*/62public CertificateX509Key(PublicKey key) {63this.key = key;64}6566/**67* Create the object, decoding the values from the passed DER stream.68*69* @param in the DerInputStream to read the X509Key from.70* @exception IOException on decoding errors.71*/72public CertificateX509Key(DerInputStream in) throws IOException {73DerValue val = in.getDerValue();74key = X509Key.parse(val);75}7677/**78* Create the object, decoding the values from the passed stream.79*80* @param in the InputStream to read the X509Key from.81* @exception IOException on decoding errors.82*/83public CertificateX509Key(InputStream in) throws IOException {84DerValue val = new DerValue(in);85key = X509Key.parse(val);86}8788/**89* Return the key as printable string.90*/91public String toString() {92if (key == null) return "";93return(key.toString());94}9596/**97* Encode the key in DER form to the stream.98*99* @param out the OutputStream to marshal the contents to.100* @exception IOException on errors.101*/102public void encode(OutputStream out) throws IOException {103DerOutputStream tmp = new DerOutputStream();104tmp.write(key.getEncoded());105106out.write(tmp.toByteArray());107}108109/**110* Set the attribute value.111*/112public void set(String name, Object obj) throws IOException {113if (name.equalsIgnoreCase(KEY)) {114this.key = (PublicKey)obj;115} else {116throw new IOException("Attribute name not recognized by " +117"CertAttrSet: CertificateX509Key.");118}119}120121/**122* Get the attribute value.123*/124public PublicKey get(String name) throws IOException {125if (name.equalsIgnoreCase(KEY)) {126return(key);127} else {128throw new IOException("Attribute name not recognized by " +129"CertAttrSet: CertificateX509Key.");130}131}132133/**134* Delete the attribute value.135*/136public void delete(String name) throws IOException {137if (name.equalsIgnoreCase(KEY)) {138key = null;139} else {140throw new IOException("Attribute name not recognized by " +141"CertAttrSet: CertificateX509Key.");142}143}144145/**146* Return an enumeration of names of attributes existing within this147* attribute.148*/149public Enumeration<String> getElements() {150AttributeNameEnumeration elements = new AttributeNameEnumeration();151elements.addElement(KEY);152153return(elements.elements());154}155156/**157* Return the name of this attribute.158*/159public String getName() {160return(NAME);161}162}163164165