pkcs9attribute.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 794 行 · 第 1/2 页
JAVA
794 行
throws IllegalArgumentException { ObjectIdentifier oid = getOID(name); if (oid == null) throw new IllegalArgumentException( "Unrecognized attribute name " + name + " constructing PKCS9Attribute."); init(oid, value); } private void init(ObjectIdentifier oid, Object value) throws IllegalArgumentException { index = indexOf(oid, PKCS9_OIDS, 1); if (index == -1) throw new IllegalArgumentException( "Unsupported OID " + oid + " constructing PKCS9Attribute."); if (!VALUE_CLASSES[index].isInstance(value)) throw new IllegalArgumentException( "Wrong value class " + " for attribute " + oid + " constructing PKCS9Attribute; was " + value.getClass().toString() + ", should be " + VALUE_CLASSES[index].toString()); this.value = value; } /** * Construct a PKCS9Attribute from its encoding on an input * stream. * * @param val the DerValue representing the DER encoding of the attribute. * @exception IOException on parsing error. */ public PKCS9Attribute(DerValue derVal) throws IOException { DerInputStream derIn = new DerInputStream(derVal.toByteArray()); DerValue[] val = derIn.getSequence(2); if (derIn.available() != 0) throw new IOException("Excess data parsing PKCS9Attribute"); if (val.length != 2) throw new IOException("PKCS9Attribute doesn't have two components"); // get the oid ObjectIdentifier oid = val[0].getOID(); index = indexOf(oid, PKCS9_OIDS, 1); if (index == -1) throw new IOException("Invalid OID for PKCS9 attribute: " + oid); DerValue[] elems = new DerInputStream(val[1].toByteArray()).getSet(1); // check single valued have only one value if (SINGLE_VALUED[index] && elems.length > 1) throwSingleValuedException(); // check for illegal element tags Byte tag; for (int i=0; i < elems.length; i++) { tag = new Byte(elems[i].tag); if (indexOf(tag, PKCS9_VALUE_TAGS[index], 0) == -1) throwTagException(tag); } switch (index) { case 1: // email address case 2: // unstructured name case 8: // unstructured address { // open scope String[] values = new String[elems.length]; for (int i=0; i < elems.length; i++) values[i] = elems[i].getAsString(); value = values; } // close scope break; case 3: // content type value = elems[0].getOID(); break; case 4: // message digest value = elems[0].getOctetString(); break; case 5: // signing time value = (new DerInputStream(elems[0].toByteArray())).getUTCTime(); break; case 6: // countersignature { // open scope SignerInfo[] values = new SignerInfo[elems.length]; for (int i=0; i < elems.length; i++) values[i] = new SignerInfo(elems[i].toDerInputStream()); value = values; } // close scope break; case 7: // challenge password value = elems[0].getAsString(); break; case 9: // extended-certificate attribute -- not supported throw new IOException("PKCS9 extended-certificate " + "attribute not supported."); // break unnecessary case 10: // issuerAndserialNumber attribute -- not supported throw new IOException("PKCS9 IssuerAndSerialNumber" + "attribute not supported."); // break unnecessary case 11: // RSA DSI proprietary case 12: // RSA DSI proprietary throw new IOException("PKCS9 RSA DSI attributes" + "11 and 12, not supported."); // break unnecessary case 13: // S/MIME unused attribute throw new IOException("PKCS9 attribute #13 not supported."); // break unnecessary case 14: // ExtensionRequest value = new CertificateExtensions( new DerInputStream(elems[0].toByteArray())); break; case 15: // SMIME-capability attribute -- not supported throw new IOException("PKCS9 SMIMECapability " + "attribute not supported."); // break unnecessary default: // can't happen } } /** * Write the DER encoding of this attribute to an output stream. * * <P> N.B.: This method always encodes values of * ChallengePassword and UnstructuredAddress attributes as ASN.1 * <code>PrintableString</code>s, without checking whether they * should be encoded as <code>T61String</code>s. */ public void derEncode(OutputStream out) throws IOException { DerOutputStream temp = new DerOutputStream(); temp.putOID(getOID()); switch (index) { case 1: // email address case 2: // unstructured name { // open scope String[] values = (String[]) value; DerOutputStream[] temps = new DerOutputStream[values.length]; for (int i=0; i < values.length; i++) { temps[i] = new DerOutputStream(); temps[i].putIA5String( values[i]); } temp.putOrderedSetOf(DerValue.tag_Set, temps); } // close scope break; case 3: // content type { DerOutputStream temp2 = new DerOutputStream(); temp2.putOID((ObjectIdentifier) value); temp.write(DerValue.tag_Set, temp2.toByteArray()); } break; case 4: // message digest { DerOutputStream temp2 = new DerOutputStream(); temp2.putOctetString((byte[]) value); temp.write(DerValue.tag_Set, temp2.toByteArray()); } break; case 5: // signing time { DerOutputStream temp2 = new DerOutputStream(); temp2.putUTCTime((Date) value); temp.write(DerValue.tag_Set, temp2.toByteArray()); } break; case 6: // countersignature temp.putOrderedSetOf(DerValue.tag_Set, (DerEncoder[]) value); break; case 7: // challenge password { DerOutputStream temp2 = new DerOutputStream(); temp2.putPrintableString((String) value); temp.write(DerValue.tag_Set, temp2.toByteArray()); } break; case 8: // unstructured address { // open scope String[] values = (String[]) value; DerOutputStream[] temps = new DerOutputStream[values.length]; for (int i=0; i < values.length; i++) { temps[i] = new DerOutputStream(); temps[i].putPrintableString(values[i]); } temp.putOrderedSetOf(DerValue.tag_Set, temps); } // close scope break; case 9: // extended-certificate attribute -- not supported throw new IOException("PKCS9 extended-certificate " + "attribute not supported."); // break unnecessary case 10: // issuerAndserialNumber attribute -- not supported throw new IOException("PKCS9 IssuerAndSerialNumber" + "attribute not supported."); // break unnecessary case 11: // RSA DSI proprietary case 12: // RSA DSI proprietary throw new IOException("PKCS9 RSA DSI attributes" + "11 and 12, not supported."); // break unnecessary case 13: // S/MIME unused attribute throw new IOException("PKCS9 attribute #13 not supported."); // break unnecessary case 14: // ExtensionRequest { DerOutputStream temp2 = new DerOutputStream(); CertificateExtensions exts = (CertificateExtensions)value; try { exts.encode(temp2, true); } catch (CertificateException ex) { throw new IOException(ex.toString()); } temp.write(DerValue.tag_Set, temp2.toByteArray()); } break; case 15: // SMIMECapability throw new IOException("PKCS9 attribute #15 not supported."); // break unnecessary default: // can't happen } DerOutputStream derOut = new DerOutputStream(); derOut.write(DerValue.tag_Sequence, temp.toByteArray()); out.write(derOut.toByteArray()); } /** * Get the value of this attribute. If the attribute is * single-valued, return just the one value. If the attribute is * multi-valued, return an array containing all the values. * It is possible for this array to be of length 0. * * <P> The * <a href=#classTable>table</a> gives the class of the value returned, * depending on the type of this attribute. */ public Object getValue() { return value; } /** * Show whether this attribute is single-valued. */ public boolean isSingleValued() { return SINGLE_VALUED[index]; } /** * Return the OID of this attribute. */ public ObjectIdentifier getOID() { return PKCS9_OIDS[index]; } /** * Return the name of this attribute. */ public String getName() { return (String) OID_NAME_TABLE.get(PKCS9_OIDS[index]); } /** * Return the OID for a given attribute name or null if we don't recognize * the name. */ public static ObjectIdentifier getOID(String name) { return (ObjectIdentifier) NAME_OID_TABLE.get(name.toLowerCase()); } /** * Return the attribute name for a given OID or null if we don't recognize * the oid. */ public static String getName(ObjectIdentifier oid) { return (String) OID_NAME_TABLE.get(oid); } /** * Returns a string representation of this attribute. */ public String toString() { StringBuffer buf = new StringBuffer(100); buf.append("["); buf.append(OID_NAME_TABLE.get(PKCS9_OIDS[index])); buf.append(": "); if (SINGLE_VALUED[index]) { if (value instanceof byte[]) { // special case for octet string HexDumpEncoder hexDump = new HexDumpEncoder(); buf.append(hexDump.encodeBuffer((byte[]) value)); } else { buf.append(value.toString()); } buf.append("]"); return buf.toString(); } else { // multi-valued boolean first = true; Object[] values = (Object[]) value; for (int j=0; j < values.length; j++) { if (first) first = false; else buf.append(", "); buf.append(values[j].toString()); } return buf.toString(); } } /** * Beginning the search at <code>start</code>, find the first * index <code>i</code> such that <code>a[i] = obj</code>. * * @return the index, if found, and -1 otherwise. */ static int indexOf(Object obj, Object[] a, int start) { for (int i=start; i < a.length; i++) { if (obj.equals(a[i])) return i; } return -1; } /** * Throw an exception when there are multiple values for * a single-valued attribute. */ private void throwSingleValuedException() throws IOException { throw new IOException("Single-value attribute " + getOID() + " (" + getName() + ")" + " has multiple values."); } /** * Throw an exception when the tag on a value encoding is * wrong for the attribute whose value it is. */ private void throwTagException(Byte tag) throws IOException { Byte[] expectedTags = PKCS9_VALUE_TAGS[index]; StringBuffer msg = new StringBuffer(100); msg.append("Value of attribute "); msg.append(getOID().toString()); msg.append(" ("); msg.append(getName()); msg.append(") has wrong tag: "); msg.append(tag.toString()); msg.append(". Expected tags: "); msg.append(expectedTags[0].toString()); for (int i = 1; i < expectedTags.length; i++) { msg.append(", "); msg.append(expectedTags[i].toString()); } msg.append("."); throw new IOException(msg.toString()); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?