Path: blob/master/src/java.base/share/classes/sun/security/pkcs10/PKCS10Attributes.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.pkcs10;2627import java.io.IOException;28import java.io.OutputStream;29import java.security.cert.CertificateException;30import java.util.Collection;31import java.util.Collections;32import java.util.Enumeration;33import java.util.Hashtable;3435import sun.security.util.*;3637/**38* This class defines the PKCS10 attributes for the request.39* The ASN.1 syntax for this is:40* <pre>41* Attributes ::= SET OF Attribute42* </pre>43*44* @author Amit Kapoor45* @author Hemma Prafullchandra46* @see PKCS1047* @see PKCS10Attribute48*/49public class PKCS10Attributes implements DerEncoder {5051private Hashtable<String, PKCS10Attribute> map =52new Hashtable<String, PKCS10Attribute>(3);5354/**55* Default constructor for the PKCS10 attribute.56*/57public PKCS10Attributes() { }5859/**60* Create the object from the array of PKCS10Attribute objects.61*62* @param attrs the array of PKCS10Attribute objects.63*/64public PKCS10Attributes(PKCS10Attribute[] attrs) {65for (int i = 0; i < attrs.length; i++) {66map.put(attrs[i].getAttributeId().toString(), attrs[i]);67}68}6970/**71* Create the object, decoding the values from the passed DER stream.72* The DER stream contains the SET OF Attribute.73*74* @param in the DerInputStream to read the attributes from.75* @exception IOException on decoding errors.76*/77public PKCS10Attributes(DerInputStream in) throws IOException {78DerValue[] attrs = in.getSet(3, true);7980if (attrs == null)81throw new IOException("Illegal encoding of attributes");82for (int i = 0; i < attrs.length; i++) {83PKCS10Attribute attr = new PKCS10Attribute(attrs[i]);84map.put(attr.getAttributeId().toString(), attr);85}86}8788/**89* Encode the attributes in DER form to the stream.90*91* @param out the OutputStream to marshal the contents to.92* @exception IOException on encoding errors.93*/94public void encode(OutputStream out) throws IOException {95derEncode(out);96}9798/**99* Encode the attributes in DER form to the stream.100* Implements the {@code DerEncoder} interface.101*102* @param out the OutputStream to marshal the contents to.103* @exception IOException on encoding errors.104*/105public void derEncode(OutputStream out) throws IOException {106// first copy the elements into an array107Collection<PKCS10Attribute> allAttrs = map.values();108PKCS10Attribute[] attribs =109allAttrs.toArray(new PKCS10Attribute[map.size()]);110111DerOutputStream attrOut = new DerOutputStream();112attrOut.putOrderedSetOf(DerValue.createTag(DerValue.TAG_CONTEXT,113true, (byte)0),114attribs);115out.write(attrOut.toByteArray());116}117118/**119* Set the attribute value.120*/121public void setAttribute(String name, Object obj) {122if (obj instanceof PKCS10Attribute) {123map.put(name, (PKCS10Attribute)obj);124}125}126127/**128* Get the attribute value.129*/130public Object getAttribute(String name) {131return map.get(name);132}133134/**135* Delete the attribute value.136*/137public void deleteAttribute(String name) {138map.remove(name);139}140141/**142* Return an enumeration of names of attributes existing within this143* attribute.144*/145public Enumeration<PKCS10Attribute> getElements() {146return (map.elements());147}148149/**150* Return a Collection of attributes existing within this151* PKCS10Attributes object.152*/153public Collection<PKCS10Attribute> getAttributes() {154return (Collections.unmodifiableCollection(map.values()));155}156157/**158* Compares this PKCS10Attributes for equality with the specified159* object. If the {@code other} object is an160* {@code instanceof PKCS10Attributes}, then161* all the entries are compared with the entries from this.162*163* @param other the object to test for equality with this PKCS10Attributes.164* @return true if all the entries match that of the Other,165* false otherwise.166*/167public boolean equals(Object other) {168if (this == other)169return true;170if (!(other instanceof PKCS10Attributes))171return false;172173Collection<PKCS10Attribute> othersAttribs =174((PKCS10Attributes)other).getAttributes();175PKCS10Attribute[] attrs =176othersAttribs.toArray(new PKCS10Attribute[othersAttribs.size()]);177int len = attrs.length;178if (len != map.size())179return false;180PKCS10Attribute thisAttr, otherAttr;181String key = null;182for (int i=0; i < len; i++) {183otherAttr = attrs[i];184key = otherAttr.getAttributeId().toString();185186if (key == null)187return false;188thisAttr = map.get(key);189if (thisAttr == null)190return false;191if (! thisAttr.equals(otherAttr))192return false;193}194return true;195}196197/**198* Returns a hashcode value for this PKCS10Attributes.199*200* @return the hashcode value.201*/202public int hashCode() {203return map.hashCode();204}205206/**207* Returns a string representation of this {@code PKCS10Attributes} object208* in the form of a set of entries, enclosed in braces and separated209* by the ASCII characters "<code>, </code>" (comma and space).210* <p>Overrides the {@code toString} method of {@code Object}.211*212* @return a string representation of this PKCS10Attributes.213*/214public String toString() {215String s = map.size() + "\n" + map.toString();216return s;217}218}219220221