diff options
author | Ade Lee <alee@redhat.com> | 2012-01-11 13:58:57 -0500 |
---|---|---|
committer | Ade Lee <alee@redhat.com> | 2012-01-11 13:58:57 -0500 |
commit | fbbf6c77236902e726faafe380a5ddf1891e8dc9 (patch) | |
tree | 1e671265cbd3d4072838eeec51b6a2ec77bed7d1 /pki/base/util/src/netscape/security/extensions/GenericASN1Extension.java | |
parent | f7a1d6a79d1b0367e556d5c53fe5e0c07c7b5c66 (diff) | |
download | pki-fbbf6c77236902e726faafe380a5ddf1891e8dc9.tar.gz pki-fbbf6c77236902e726faafe380a5ddf1891e8dc9.tar.xz pki-fbbf6c77236902e726faafe380a5ddf1891e8dc9.zip |
Formatting - no wrap in comments and code
Diffstat (limited to 'pki/base/util/src/netscape/security/extensions/GenericASN1Extension.java')
-rw-r--r-- | pki/base/util/src/netscape/security/extensions/GenericASN1Extension.java | 319 |
1 files changed, 152 insertions, 167 deletions
diff --git a/pki/base/util/src/netscape/security/extensions/GenericASN1Extension.java b/pki/base/util/src/netscape/security/extensions/GenericASN1Extension.java index 116977da..f9ac337c 100644 --- a/pki/base/util/src/netscape/security/extensions/GenericASN1Extension.java +++ b/pki/base/util/src/netscape/security/extensions/GenericASN1Extension.java @@ -38,13 +38,11 @@ import netscape.security.x509.CertAttrSet; import netscape.security.x509.Extension; import netscape.security.x509.OIDMap; - - /** * Represent the AsnInteger Extension. */ -public class GenericASN1Extension extends Extension -implements CertAttrSet { +public class GenericASN1Extension extends Extension + implements CertAttrSet { public String getName() { return name; } @@ -77,25 +75,22 @@ implements CertAttrSet { /** * Identifier for this attribute, to be used with the * get, set, delete methods of Certificate, x509 type. - */ + */ private String name; public static String OID = null; public static Hashtable<String, String> mConfig = null; public static String pattern = null; private int index = 0; - // Encode this value - private void encodeThis() - throws IOException, ParseException - { + private void encodeThis() + throws IOException, ParseException { this.extensionValue = encodePattern(); } - + // Encode pattern - private byte[] encodePattern() - throws IOException, ParseException - { + private byte[] encodePattern() + throws IOException, ParseException { DerOutputStream os = new DerOutputStream(); DerOutputStream tmp = new DerOutputStream(); String type = null; @@ -104,87 +99,77 @@ implements CertAttrSet { while (index < pattern.length()) { char ch = pattern.charAt(index); switch (ch) { - case '{' : - index++; - byte[] buff = encodePattern(); - tmp.putDerValue(new DerValue(buff)); - break; - case '}' : - os.write(DerValue.tag_Sequence, tmp); - return os.toByteArray(); - default : - type = (String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_TYPE); - if (type.equalsIgnoreCase("integer")) { - int num = Integer.parseInt((String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_VALUE)); - PutInteger(tmp, num); - } - else if (type.equalsIgnoreCase("ia5string")) { - source = (String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_SOURCE); - value = (String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_VALUE); - if (source.equalsIgnoreCase("file")) - PutIA5String(tmp, getFromFile(value)); - else - PutIA5String(tmp, value); - } - else if (type.equalsIgnoreCase("octetstring")) { - source = (String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_SOURCE); - value = (String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_VALUE); - // It should be colon seperated ASCII Hexdecimal String - if (source.equalsIgnoreCase("file")) - PutOctetString(tmp, getFromFile(value)); - else - PutOctetString(tmp, value); - } - else if (type.equalsIgnoreCase("bmpstring")) { - source = (String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_SOURCE); - value = (String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_VALUE); - if (source.equalsIgnoreCase("file")) - PutBMPString(tmp, getFromFile(value)); - else - PutBMPString(tmp, value); - } - else if (type.equalsIgnoreCase("printablestring")) { - source = (String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_SOURCE); - value = (String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_VALUE); - if (source.equalsIgnoreCase("file")) - PutPrintableString(tmp, getFromFile(value)); - else - PutPrintableString(tmp, value); - } - else if (type.equalsIgnoreCase("visiblestring")) { - source = (String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_SOURCE); - value = (String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_VALUE); - if (source.equalsIgnoreCase("file")) - PutVisibleString(tmp, getFromFile(value)); - else - PutVisibleString(tmp, value); - } - else if (type.equalsIgnoreCase("utctime")) { - value = (String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_VALUE); - PutUTCtime(tmp, value); - } - else if (type.equalsIgnoreCase("oid")) { - value = (String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_VALUE); - PutOID(tmp, value); - } - else if (type.equalsIgnoreCase("boolean")) { - boolean bool = false; - String b = (String)mConfig.get(PROP_ATTRIBUTE+"."+ch+"."+PROP_VALUE); - if (b.equalsIgnoreCase("true")) - bool = true; - else - bool = false; - PutBoolean(tmp, bool); - } - else if (type.equalsIgnoreCase("null")) { - tmp.putNull(); - } - else { - throw new ParseException("Unknown Attribute Type", 0); - } + case '{': + index++; + byte[] buff = encodePattern(); + tmp.putDerValue(new DerValue(buff)); + break; + case '}': + os.write(DerValue.tag_Sequence, tmp); + return os.toByteArray(); + default: + type = (String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_TYPE); + if (type.equalsIgnoreCase("integer")) { + int num = Integer.parseInt((String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_VALUE)); + PutInteger(tmp, num); + } else if (type.equalsIgnoreCase("ia5string")) { + source = (String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_SOURCE); + value = (String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_VALUE); + if (source.equalsIgnoreCase("file")) + PutIA5String(tmp, getFromFile(value)); + else + PutIA5String(tmp, value); + } else if (type.equalsIgnoreCase("octetstring")) { + source = (String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_SOURCE); + value = (String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_VALUE); + // It should be colon seperated ASCII Hexdecimal String + if (source.equalsIgnoreCase("file")) + PutOctetString(tmp, getFromFile(value)); + else + PutOctetString(tmp, value); + } else if (type.equalsIgnoreCase("bmpstring")) { + source = (String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_SOURCE); + value = (String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_VALUE); + if (source.equalsIgnoreCase("file")) + PutBMPString(tmp, getFromFile(value)); + else + PutBMPString(tmp, value); + } else if (type.equalsIgnoreCase("printablestring")) { + source = (String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_SOURCE); + value = (String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_VALUE); + if (source.equalsIgnoreCase("file")) + PutPrintableString(tmp, getFromFile(value)); + else + PutPrintableString(tmp, value); + } else if (type.equalsIgnoreCase("visiblestring")) { + source = (String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_SOURCE); + value = (String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_VALUE); + if (source.equalsIgnoreCase("file")) + PutVisibleString(tmp, getFromFile(value)); + else + PutVisibleString(tmp, value); + } else if (type.equalsIgnoreCase("utctime")) { + value = (String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_VALUE); + PutUTCtime(tmp, value); + } else if (type.equalsIgnoreCase("oid")) { + value = (String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_VALUE); + PutOID(tmp, value); + } else if (type.equalsIgnoreCase("boolean")) { + boolean bool = false; + String b = (String) mConfig.get(PROP_ATTRIBUTE + "." + ch + "." + PROP_VALUE); + if (b.equalsIgnoreCase("true")) + bool = true; + else + bool = false; + PutBoolean(tmp, bool); + } else if (type.equalsIgnoreCase("null")) { + tmp.putNull(); + } else { + throw new ParseException("Unknown Attribute Type", 0); + } } index++; - } + } return tmp.toByteArray(); } @@ -192,22 +177,22 @@ implements CertAttrSet { /** * Create a GenericASN1Extension with the value and oid. * The criticality is set to false. - * + * * @param the values to be set for the extension. */ public GenericASN1Extension(String name, String oid, String pattern, boolean critical, Hashtable<String, String> config) - throws IOException, ParseException - { + throws IOException, ParseException { ObjectIdentifier tmpid = new ObjectIdentifier(oid); this.name = name; OID = oid; mConfig = config; this.pattern = pattern; - + try { if (OIDMap.getName(tmpid) == null) OIDMap.addAttribute("netscape.security.x509.GenericASN1Extension", oid, name); - } catch (CertificateException e) {} + } catch (CertificateException e) { + } this.extensionId = tmpid; this.critical = critical; @@ -217,51 +202,50 @@ implements CertAttrSet { /** * Create a GenericASN1Extension with the value and oid. * The criticality is set to false. - * + * * @param the values to be set for the extension. */ public GenericASN1Extension(Hashtable<String, String> config) - throws IOException, ParseException - { + throws IOException, ParseException { mConfig = config; - ObjectIdentifier tmpid = new ObjectIdentifier((String)mConfig.get(PROP_OID)); - this.name = (String)mConfig.get(PROP_NAME); - OID = (String)mConfig.get(PROP_OID); - pattern = (String)mConfig.get(PROP_PATTERN); - + ObjectIdentifier tmpid = new ObjectIdentifier((String) mConfig.get(PROP_OID)); + this.name = (String) mConfig.get(PROP_NAME); + OID = (String) mConfig.get(PROP_OID); + pattern = (String) mConfig.get(PROP_PATTERN); + try { if (OIDMap.getName(tmpid) == null) OIDMap.addAttribute("GenericASN1Extension", OID, this.name); - } catch (CertificateException e) {} + } catch (CertificateException e) { + } this.extensionId = tmpid; this.critical = false; - String b = (String)mConfig.get(PROP_CRITICAL); - if (b.equalsIgnoreCase("true")) - this.critical = true; - else - this.critical = false; + String b = (String) mConfig.get(PROP_CRITICAL); + if (b.equalsIgnoreCase("true")) + this.critical = true; + else + this.critical = false; encodeThis(); } /** * Create the extension from the passed DER encoded value of the same. - * + * * @param critical true if the extension is to be treated as critical. * @param value Array of DER encoded bytes of the actual value. * @exception IOException on error. */ public GenericASN1Extension(Boolean critical, Object value) - throws IOException - { + throws IOException { this.extensionId = new ObjectIdentifier(OID); this.critical = critical.booleanValue(); int len = Array.getLength(value); - byte[] extValue = new byte[len]; - for (int i = 0; i < len; i++) { - extValue[i] = Array.getByte(value, i); - } + byte[] extValue = new byte[len]; + for (int i = 0; i < len; i++) { + extValue[i] = Array.getByte(value, i); + } this.extensionValue = extValue; } @@ -295,7 +279,7 @@ implements CertAttrSet { /** * Decode the extension from the InputStream. - * + * * @param in the InputStream to unmarshal the contents from. * @exception IOException on decoding or validity errors. */ @@ -305,14 +289,13 @@ implements CertAttrSet { /** * Write the extension to the DerOutputStream. - * + * * @param out the DerOutputStream to write the extension to. * @exception IOException on encoding errors. */ - public void encode(OutputStream out) - throws IOException - { - DerOutputStream tmp = new DerOutputStream(); + public void encode(OutputStream out) + throws IOException { + DerOutputStream tmp = new DerOutputStream(); try { if (this.extensionValue == null) { @@ -320,106 +303,107 @@ implements CertAttrSet { this.critical = true; encodeThis(); } + } catch (ParseException e) { } - catch (ParseException e) {} - + super.encode(tmp); out.write(tmp.toByteArray()); } - /** * Set the name of this attribute. */ - public void setName (String name) { + public void setName(String name) { this.name = name; } - + /** * Return the OID of this attribute. */ - public String getOID () { + public String getOID() { return (OID); } + /** * Set the OID of this attribute. */ - public void setOID (String oid) { + public void setOID(String oid) { OID = oid; } + /** * Return an enumeration of names of attributes existing within this * attribute. */ - public Enumeration<String> getAttributeNames () { + public Enumeration<String> getAttributeNames() { Vector<String> elements = new Vector<String>(); elements.addElement("octet"); - return (elements.elements()); + return (elements.elements()); } - + private void PutInteger(DerOutputStream os, int number) - throws IOException, ParseException { + throws IOException, ParseException { os.putInteger(new BigInt(number)); return; } - - private void PutIA5String(DerOutputStream os, String value) - throws IOException, ParseException { + + private void PutIA5String(DerOutputStream os, String value) + throws IOException, ParseException { os.putIA5String(value); return; } - private void PutOctetString(DerOutputStream os, String value) - throws IOException, ParseException { + private void PutOctetString(DerOutputStream os, String value) + throws IOException, ParseException { StringTokenizer token = new StringTokenizer(value, ":"); byte[] octets = new byte[token.countTokens()]; for (int i = 0; token.hasMoreElements(); i++) { - String num = (String)token.nextElement(); + String num = (String) token.nextElement(); octets[i] = (byte) Integer.parseInt(num, 16); } - + os.putOctetString(octets); return; } - private void PutBMPString(DerOutputStream os, String value) - throws IOException, ParseException { + private void PutBMPString(DerOutputStream os, String value) + throws IOException, ParseException { os.putBMPString(value); return; } - private void PutPrintableString(DerOutputStream os, String value) - throws IOException, ParseException { + private void PutPrintableString(DerOutputStream os, String value) + throws IOException, ParseException { os.putPrintableString(value); return; } - private void PutVisibleString(DerOutputStream os, String value) - throws IOException, ParseException { + private void PutVisibleString(DerOutputStream os, String value) + throws IOException, ParseException { os.putVisibleString(value); return; } - private void PutUTCtime(DerOutputStream os, String value) - throws IOException, ParseException { + private void PutUTCtime(DerOutputStream os, String value) + throws IOException, ParseException { DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); os.putUTCTime(df.parse(value)); return; } - private void PutOID(DerOutputStream os, String value) - throws IOException, ParseException { + private void PutOID(DerOutputStream os, String value) + throws IOException, ParseException { os.putOID(new ObjectIdentifier(value)); return; } private void PutBoolean(DerOutputStream os, boolean value) - throws IOException, ParseException { + throws IOException, ParseException { os.putBoolean(value); return; } - + private String getFromFile(String fname) throws IOException { String s = null; byte[] buff = null; @@ -427,33 +411,34 @@ implements CertAttrSet { int j = 0; if ((fname == null) || (fname.equals(""))) { throw new IOException("File name is not provided."); - } - + } + FileInputStream fis = new FileInputStream(fname); int n = 0; while ((n = fis.available()) > 0) { buff = new byte[n]; int result = fis.read(buff); - if (result == -1) break; + if (result == -1) + break; s = new String(buff); } - + for (i = 0, j = 0; j < s.length(); j++) { - int ch = (int)s.charAt(j); + int ch = (int) s.charAt(j); if (ch == 10 || ch == 13 || ch == 9) continue; i++; - } + } buff = new byte[i]; for (i = 0, j = 0; j < s.length(); j++) { - int ch = (int)s.charAt(j); + int ch = (int) s.charAt(j); if (ch == 10 || ch == 13 || ch == 9) continue; - buff[i++] = (byte)ch; - } - + buff[i++] = (byte) ch; + } + s = new String(buff); - - return s; - } + + return s; + } } |