diff options
Diffstat (limited to 'pki/base/util/src/netscape/security/pkcs/PKCS9Attributes.java')
-rw-r--r-- | pki/base/util/src/netscape/security/pkcs/PKCS9Attributes.java | 350 |
1 files changed, 183 insertions, 167 deletions
diff --git a/pki/base/util/src/netscape/security/pkcs/PKCS9Attributes.java b/pki/base/util/src/netscape/security/pkcs/PKCS9Attributes.java index 97769308d..aff0ee0f7 100644 --- a/pki/base/util/src/netscape/security/pkcs/PKCS9Attributes.java +++ b/pki/base/util/src/netscape/security/pkcs/PKCS9Attributes.java @@ -27,166 +27,180 @@ import netscape.security.util.DerOutputStream; import netscape.security.util.DerValue; import netscape.security.util.ObjectIdentifier; + /** - * A set of attributes of class PKCS9Attribute. - * + * A set of attributes of class PKCS9Attribute. + * * @version 1.2 97/12/10 * @author Douglas Hoover */ public class PKCS9Attributes { /** - * Attributes in this set indexed by OID. + * Attributes in this set indexed by OID. */ private final Hashtable attributes = new Hashtable(3); /** - * The keys of this hashtable are the OIDs of permitted attributes. + * The keys of this hashtable are the OIDs of permitted attributes. */ private final Hashtable permittedAttributes; /** - * The DER encoding of this attribute set. The tag byte must be + * The DER encoding of this attribute set. The tag byte must be * DerValue.tag_SetOf. - */ + */ private final byte[] derEncoding; /** - * Construct a set of PKCS9 Attributes from its DER encoding on a - * DerInputStream, accepting only attributes with OIDs on the given list. If - * the array is null, accept all attributes supported by class - * PKCS9Attribute. - * - * @param permittedAttributes Array of attribute OIDs that will be accepted. - * @param buf the contents of the DER encoding of the attribute set. - * - * @exception IOException on i/o error, encoding syntax error, unacceptable - * or unsupported attribute, or duplicate attribute. - * + * Construct a set of PKCS9 Attributes from its + * DER encoding on a DerInputStream, accepting only attributes + * with OIDs on the given + * list. If the array is null, accept all attributes supported by + * class PKCS9Attribute. + * + * @param permittedAttributes + * Array of attribute OIDs that will be accepted. + * @param buf + * the contents of the DER encoding of the attribute set. + * + * @exception IOException + * on i/o error, encoding syntax error, unacceptable or + * unsupported attribute, or duplicate attribute. + * * @see PKCS9Attribute */ public PKCS9Attributes(ObjectIdentifier[] permittedAttributes, - DerInputStream in) throws IOException { - if (permittedAttributes != null) { - this.permittedAttributes = new Hashtable(permittedAttributes.length); - - for (int i = 0; i < permittedAttributes.length; i++) - this.permittedAttributes.put(permittedAttributes[i], - permittedAttributes[i]); - } else { - this.permittedAttributes = null; - } - - // derEncoding initialized in <code>decode()</code> - derEncoding = decode(in); + DerInputStream in) throws IOException { + if (permittedAttributes != null) { + this.permittedAttributes = + new Hashtable(permittedAttributes.length); + + for (int i = 0; i < permittedAttributes.length; i++) + this.permittedAttributes.put(permittedAttributes[i], + permittedAttributes[i]); + } else { + this.permittedAttributes = null; + } + + // derEncoding initialized in <code>decode()</code> + derEncoding = decode(in); } /** - * Construct a set of PKCS9 Attributes from its contents of its DER encoding - * on a DerInputStream. Accept all attributes supported by class - * PKCS9Attribute. - * - * @exception IOException on i/o error, encoding syntax error, or - * unsupported or duplicate attribute. - * + * Construct a set of PKCS9 Attributes from its contents of its + * DER encoding on a DerInputStream. Accept all attributes + * supported by class PKCS9Attribute. + * + * @exception IOException + * on i/o error, encoding syntax error, or unsupported or + * duplicate attribute. + * * @see PKCS9Attribute */ public PKCS9Attributes(DerInputStream in) throws IOException { - // anything goes - // derEncoding initialized in <code>decode()</code> - derEncoding = decode(in); - permittedAttributes = null; + // anything goes + // derEncoding initialized in <code>decode()</code> + derEncoding = decode(in); + permittedAttributes = null; } /** - * Construct a set of PKCS9 Attributes from the given array of PCK9 - * attributes. DER encoding on a DerInputStream. All attributes in - * <code>attribs</code> must be supported by class PKCS9Attribute. - * - * @exception IOException on i/o error, encoding syntax error, or - * unsupported or duplicate attribute. - * + * Construct a set of PKCS9 Attributes from the given array of + * PCK9 attributes. + * DER encoding on a DerInputStream. All attributes in + * <code>attribs</code> must be + * supported by class PKCS9Attribute. + * + * @exception IOException + * on i/o error, encoding syntax error, or unsupported or + * duplicate attribute. + * * @see PKCS9Attribute */ - public PKCS9Attributes(PKCS9Attribute[] attribs) - throws IllegalArgumentException, IOException { - ObjectIdentifier oid; - for (int i = 0; i < attribs.length; i++) { - oid = attribs[i].getOID(); - if (attributes.containsKey(oid)) - throw new IllegalArgumentException("PKCSAttribute " - + attribs[i].getOID() - + " duplicated while constructing " - + "PKCS9Attributes."); - - attributes.put(oid, attribs[i]); - } - derEncoding = generateDerEncoding(); - permittedAttributes = null; + public PKCS9Attributes(PKCS9Attribute[] attribs) + throws IllegalArgumentException, IOException { + ObjectIdentifier oid; + for (int i=0; i < attribs.length; i++) { + oid = attribs[i].getOID(); + if (attributes.containsKey(oid)) + throw new IllegalArgumentException( + "PKCSAttribute " + attribs[i].getOID() + + " duplicated while constructing " + + "PKCS9Attributes."); + + attributes.put(oid, attribs[i]); + } + derEncoding = generateDerEncoding(); + permittedAttributes = null; } + /** - * Decode this set of PKCS9 attribute set from the contents of its DER - * encoding. - * - * @param buf the contents of the DER encoding of the attribute set. - * - * @exception IOException on i/o error, encoding syntax error, unacceptable - * or unsupported attribute, or duplicate attribute. + * Decode this set of PKCS9 attribute set from the contents of its + * DER encoding. + * + * @param buf + * the contents of the DER encoding of the attribute set. + * + * @exception IOException + * on i/o error, encoding syntax error, unacceptable or + * unsupported attribute, or duplicate attribute. */ private byte[] decode(DerInputStream in) throws IOException { - DerValue val = in.getDerValue(); + DerValue val = in.getDerValue(); - // save the DER encoding with its proper tag byte. - byte[] derEncoding = val.toByteArray(); - derEncoding[0] = DerValue.tag_SetOf; + // save the DER encoding with its proper tag byte. + byte[] derEncoding = val.toByteArray(); + derEncoding[0] = DerValue.tag_SetOf; - DerInputStream derIn = new DerInputStream(derEncoding); - DerValue[] derVals = derIn.getSet(3, true); + DerInputStream derIn = new DerInputStream(derEncoding); + DerValue[] derVals = derIn.getSet(3,true); - PKCS9Attribute attrib; - ObjectIdentifier oid; - int index; + PKCS9Attribute attrib; + ObjectIdentifier oid; + int index; - for (int i = 0; i < derVals.length; i++) { - attrib = new PKCS9Attribute(derVals[i]); - oid = attrib.getOID(); + for (int i=0; i < derVals.length; i++) { + attrib = new PKCS9Attribute(derVals[i]); + oid = attrib.getOID(); - if (attributes.get(oid) != null) - throw new IOException("Duplicate PKCS9 attribute: " + oid); + if (attributes.get(oid) != null) + throw new IOException("Duplicate PKCS9 attribute: " + oid); - if (permittedAttributes != null - && !permittedAttributes.containsKey(oid)) - throw new IOException("Attribute " + oid - + " not permitted in this attribute set"); - - attributes.put(oid, attrib); - } - return derEncoding; + if (permittedAttributes != null && + !permittedAttributes.containsKey(oid)) + throw new IOException("Attribute " + oid + + " not permitted in this attribute set"); + + attributes.put(oid,attrib); + } + return derEncoding; } /** - * Put the DER encoding of this PKCS9 attribute set on an DerOutputStream, - * tagged with the given implicit tag. - * + * Put the DER encoding of this PKCS9 attribute set on an + * DerOutputStream, tagged with the given implicit tag. + * * @param tag the implicit tag to use in the DER encoding. * @param out the output stream on which to put the DER encoding. - * - * @exception IOException on output error. + * + * @exception IOException on output error. */ public void encode(byte tag, OutputStream out) throws IOException { - out.write(tag); - out.write(derEncoding, 1, derEncoding.length - 1); + out.write(tag); + out.write(derEncoding, 1, derEncoding.length -1); } private byte[] generateDerEncoding() throws IOException { - DerOutputStream out = new DerOutputStream(); - Object[] attribVals = attributes.values().toArray(); - - out.putOrderedSetOf(DerValue.tag_SetOf, castToDerEncoder(attribVals)); - return out.toByteArray(); + DerOutputStream out = new DerOutputStream(); + Object[] attribVals = attributes.values().toArray(); + + out.putOrderedSetOf(DerValue.tag_SetOf, + castToDerEncoder(attribVals)); + return out.toByteArray(); } /** @@ -194,111 +208,113 @@ public class PKCS9Attributes { * DerValue.tag_SetOf. */ public byte[] getDerEncoding() throws IOException { - return (byte[]) derEncoding.clone(); - + return (byte[]) derEncoding.clone(); + } /** * Get an attribute from this set. - */ + */ public PKCS9Attribute getAttribute(ObjectIdentifier oid) { - return (PKCS9Attribute) attributes.get(oid); + return (PKCS9Attribute) attributes.get(oid); } /** * Get an attribute from this set. - */ + */ public PKCS9Attribute getAttribute(String name) { - return (PKCS9Attribute) attributes.get(PKCS9Attribute.getOID(name)); + return (PKCS9Attribute) attributes.get(PKCS9Attribute.getOID(name)); } + /** * Get an array of all attributes in this set, in order of OID. - */ + */ public PKCS9Attribute[] getAttributes() { - PKCS9Attribute[] attribs = new PKCS9Attribute[attributes.size()]; - ObjectIdentifier oid; - - int j = 0; - for (int i = 1; i < PKCS9Attribute.PKCS9_OIDS.length - && j < attribs.length; i++) { - attribs[j] = getAttribute(PKCS9Attribute.PKCS9_OIDS[i]); - - if (attribs[j] != null) - j++; - } - return attribs; + PKCS9Attribute[] attribs = new PKCS9Attribute[attributes.size()]; + ObjectIdentifier oid; + + int j = 0; + for (int i=1; i < PKCS9Attribute.PKCS9_OIDS.length && + j < attribs.length; i++) { + attribs[j] = getAttribute(PKCS9Attribute.PKCS9_OIDS[i]); + + if (attribs[j] != null) + j++; + } + return attribs; } /** * Get an attribute value by OID. */ - public Object getAttributeValue(ObjectIdentifier oid) throws IOException { - try { - Object value = getAttribute(oid).getValue(); - return value; - } catch (NullPointerException ex) { - throw new IOException("No value found for attribute " + oid); - } + public Object getAttributeValue(ObjectIdentifier oid) + throws IOException { + try { + Object value = getAttribute(oid).getValue(); + return value; + } catch (NullPointerException ex) { + throw new IOException("No value found for attribute " + oid); + } } - /** - * Get an attribute value by type name. + /** + * Get an attribute value by type name. */ - public Object getAttributeValue(String name) throws IOException { - ObjectIdentifier oid = PKCS9Attribute.getOID(name); + public Object getAttributeValue(String name) throws IOException { + ObjectIdentifier oid = PKCS9Attribute.getOID(name); - if (oid == null) - throw new IOException("Attribute name " + name - + " not recognized or not supported."); + if (oid == null) + throw new IOException("Attribute name " + name + + " not recognized or not supported."); - return getAttributeValue(oid); - } + return getAttributeValue(oid); + } - /** + + /** * Returns the PKCS9 block in a printable string form. */ public String toString() { - StringBuffer buf = new StringBuffer(200); - buf.append("PKCS9 Attributes: [\n\t"); - - ObjectIdentifier oid; - PKCS9Attribute value; + StringBuffer buf = new StringBuffer(200); + buf.append("PKCS9 Attributes: [\n\t"); + + ObjectIdentifier oid; + PKCS9Attribute value; - boolean first = true; - for (int i = 1; i < PKCS9Attribute.PKCS9_OIDS.length; i++) { - value = getAttribute(PKCS9Attribute.PKCS9_OIDS[i]); + boolean first = true; + for (int i = 1; i < PKCS9Attribute.PKCS9_OIDS.length; i++) { + value = getAttribute(PKCS9Attribute.PKCS9_OIDS[i]); - if (value == null) - continue; + if (value == null) continue; - // we have a value; print it - if (first) - first = false; - else - buf.append(";\n\t"); + // we have a value; print it + if (first) + first = false; + else + buf.append(";\n\t"); - buf.append(value.toString()); - } + buf.append(value.toString()); + } - buf.append("\n\t] (end PKCS9 Attributes)"); + buf.append("\n\t] (end PKCS9 Attributes)"); - return buf.toString(); + return buf.toString(); } - - /** - * Cast an object array whose components are <code>DerEncoder</code>s to - * <code>DerEncoder[]</code>. + + /** + * Cast an object array whose components are + * <code>DerEncoder</code>s to <code>DerEncoder[]</code>. */ static DerEncoder[] castToDerEncoder(Object[] objs) { - DerEncoder[] encoders = new DerEncoder[objs.length]; + DerEncoder[] encoders = new DerEncoder[objs.length]; - for (int i = 0; i < encoders.length; i++) - encoders[i] = (DerEncoder) objs[i]; + for (int i=0; i < encoders.length; i++) + encoders[i] = (DerEncoder) objs[i]; - return encoders; + return encoders; } } |