?? ava.java
字號:
} catch (IOException ie) { throw new IllegalArgumentException("DER Value conversion"); } typeAndValue.append('#'); for (int j = 0; j < data.length; j++) { byte b = data[j]; typeAndValue.append(Character.forDigit(0xF & (b >>> 4), 16)); typeAndValue.append(Character.forDigit(0xF & b, 16)); } } else { /* * 2.4 (cont): Otherwise, if the AttributeValue is of a type which * has a string representation, the value is converted first to a * UTF-8 string according to its syntax specification. * * NOTE: this implementation only emits DirectoryStrings of the * types returned by isDerString(). */ String valStr = null; try { valStr = new String(value.getDataBytes(), "UTF8"); } catch (IOException ie) { throw new IllegalArgumentException("DER Value conversion"); } /* * 2.4 (cont): If the UTF-8 string does not have any of the * following characters which need escaping, then that string can be * used as the string representation of the value. * * o a space or "#" character occurring at the beginning of the * string * o a space character occurring at the end of the string * * o one of the characters ",", "+", """, "\", "<", ">" or ";" * * If a character to be escaped is one of the list shown above, then * it is prefixed by a backslash ('\' ASCII 92). * * Otherwise the character to be escaped is replaced by a backslash * and two hex digits, which form a single byte in the code of the * character. */ final String escapees = ",+<>;\"\\"; StringBuffer sbuffer = new StringBuffer(); boolean previousWhite = false; for (int i = 0; i < valStr.length(); i++) { char c = valStr.charAt(i); if (DerValue.isPrintableStringChar(c) || escapees.indexOf(c) >= 0 || (i == 0 && c == '#')) { // escape leading '#' and escapees if ((i == 0 && c == '#') || escapees.indexOf(c) >= 0) { sbuffer.append('\\'); } // convert multiple whitespace to single whitespace if (!Character.isWhitespace(c)) { previousWhite = false; sbuffer.append(c); } else { if (previousWhite == false) { // add single whitespace previousWhite = true; sbuffer.append(c); } else { // ignore subsequent consecutive whitespace continue; } } } else if (debug != null && Debug.isOn("ava")) { // embed non-printable/non-escaped char // as escaped hex pairs for debugging previousWhite = false; byte valueBytes[] = null; try { valueBytes = Character.toString(c).getBytes("UTF8"); } catch (IOException ie) { throw new IllegalArgumentException ("DER Value conversion"); } for (int j = 0; j < valueBytes.length; j++) { sbuffer.append('\\'); sbuffer.append(Character.forDigit (0xF & (valueBytes[j] >>> 4), 16)); sbuffer.append(Character.forDigit (0xF & (valueBytes[j]), 16)); } } else { // append non-printable/non-escaped char previousWhite = false; sbuffer.append(c); } } // remove leading and trailing whitespace from value typeAndValue.append(sbuffer.toString().trim()); } String canon = new String(typeAndValue); canon = canon.toUpperCase(Locale.US).toLowerCase(Locale.US); return Normalizer.normalize(canon, Normalizer.DECOMP_COMPAT, 0); } /* * Return true if DerValue can be represented as a String. */ private static boolean isDerString(DerValue value, boolean canonical) { if (canonical) { switch (value.tag) { case DerValue.tag_PrintableString: case DerValue.tag_UTF8String: return true; default: return false; } } else { switch (value.tag) { case DerValue.tag_PrintableString: case DerValue.tag_T61String: case DerValue.tag_IA5String: case DerValue.tag_GeneralString: case DerValue.tag_BMPString: case DerValue.tag_UTF8String: return true; default: return false; } } } boolean hasRFC2253Keyword() { return AVAKeyword.hasKeyword(oid, RFC2253); } private String toKeywordValueString(String keyword) { /* * Construct the value with as little copying and garbage * production as practical. First the keyword (mandatory), * then the equals sign, finally the value. */ StringBuffer retval = new StringBuffer(40); retval.append(keyword); retval.append("="); try { String valStr = value.getAsString(); if (valStr == null) { // rfc1779 specifies that attribute values associated // with non-standard keyword attributes may be represented // using the hex format below. This will be used only // when the value is not a string type byte data [] = value.toByteArray(); retval.append('#'); for (int i = 0; i < data.length; i++) { retval.append(hexDigits.charAt((data [i] >> 4) & 0x0f)); retval.append(hexDigits.charAt(data [i] & 0x0f)); } } else { boolean quoteNeeded = false; StringBuffer sbuffer = new StringBuffer(); boolean previousWhite = false; /* * Special characters (e.g. AVA list separators) cause strings * to need quoting, or at least escaping. So do leading or * trailing spaces, and multiple internal spaces. */ for (int i = 0; i < valStr.length(); i++) { char c = valStr.charAt(i); if (DerValue.isPrintableStringChar(c) || specialChars.indexOf(c) >= 0) { // quote if leading whitespace or special chars if (!quoteNeeded && ((i == 0 && (c == ' ' || c == '\n')) || specialChars.indexOf(c) >= 0)) { quoteNeeded = true; } // quote if multiple internal whitespace if (!(c == ' ' || c == '\n')) { // escape '"' and '\' if (c == '"' || c == '\\') { sbuffer.append('\\'); } previousWhite = false; } else { if (!quoteNeeded && previousWhite) { quoteNeeded = true; } previousWhite = true; } sbuffer.append(c); } else if (debug != null && Debug.isOn("ava")) { // embed non-printable/non-escaped char // as escaped hex pairs for debugging previousWhite = false; // embed escaped hex pairs byte[] valueBytes = Character.toString(c).getBytes("UTF8"); for (int j = 0; j < valueBytes.length; j++) { sbuffer.append('\\'); char hexChar = Character.forDigit (0xF & (valueBytes[j] >>> 4), 16); sbuffer.append(Character.toUpperCase(hexChar)); hexChar = Character.forDigit (0xF & (valueBytes[j]), 16); sbuffer.append(Character.toUpperCase(hexChar)); } } else { // append non-printable/non-escaped char previousWhite = false; sbuffer.append(c); } } // quote if trailing whitespace if (sbuffer.length() > 0) { char trailChar = sbuffer.charAt(sbuffer.length() - 1); if (trailChar == ' ' || trailChar == '\n') { quoteNeeded = true; } } // Emit the string ... quote it if needed if (quoteNeeded) { retval.append("\"" + sbuffer.toString() + "\""); } else { retval.append(sbuffer.toString()); } } } catch (IOException e) { throw new IllegalArgumentException("DER Value conversion"); } return retval.toString(); }}/** * Helper class that allows conversion from String to ObjectIdentifier and * vice versa according to RFC1779, RFC2253, and an augmented version of * those standards. */class AVAKeyword { private static final Map oidMap, keywordMap; private String keyword; private ObjectIdentifier oid; private boolean rfc1779Compliant, rfc2253Compliant; private AVAKeyword(String keyword, ObjectIdentifier oid, boolean rfc1779Compliant, boolean rfc2253Compliant) { this.keyword = keyword; this.oid = oid; this.rfc1779Compliant = rfc1779Compliant; this.rfc2253Compliant = rfc2253Compliant; // register it oidMap.put(oid, this); keywordMap.put(keyword, this); } private boolean isCompliant(int standard) { switch (standard) { case AVA.RFC1779: return rfc1779Compliant; case AVA.RFC2253: return rfc2253Compliant; case AVA.DEFAULT: return true; default: // should not occur, internal error throw new IllegalArgumentException("Invalid standard " + standard); } } /** * Get an object identifier representing the specified keyword (or * string encoded object identifier) in the given standard. * * @throws IOException If the keyword is not valid in the specified standard */ static ObjectIdentifier getOID(String keyword, int standard) throws IOException { keyword = keyword.toUpperCase(); if (standard == AVA.RFC2253) { if (keyword.startsWith(" ") || keyword.endsWith(" ")) { throw new IOException("Invalid leading or trailing space " + "in keyword \"" + keyword + "\""); } } else { keyword = keyword.trim(); } AVAKeyword ak = (AVAKeyword)keywordMap.get(keyword); if ((ak != null) && ak.isCompliant(standard)) { return ak.oid; } // no keyword found or not standard compliant, check if OID string // RFC1779 requires, DEFAULT allows OID. prefix if (standard == AVA.RFC1779) { if (keyword.startsWith("OID.") == false) { throw new IOException("Invalid RFC1779 keyword: " + keyword); } keyword = keyword.substring(4); } else if (standard == AVA.DEFAULT) { if (keyword.startsWith("OID.")) { keyword = keyword.substring(4); } } boolean number = false; if (keyword.length() != 0) { char ch = keyword.charAt(0); if ((ch >= '0') && (ch <= '9')) { number = true; } } if (number == false) { throw new IOException("Invalid keyword \"" + keyword + "\""); } return new ObjectIdentifier(keyword); } /** * Get a keyword for the given ObjectIdentifier according to standard. * If no keyword is available, the ObjectIdentifier is encoded as a * String. */ static String getKeyword(ObjectIdentifier oid, int standard) { AVAKeyword ak = (AVAKeyword)oidMap.get(oid); if ((ak != null) && ak.isCompliant(standard)) { return ak.keyword; } // no compliant keyword, use OID String oidString = oid.toString(); if (standard == AVA.RFC2253) { return oidString; } else { return "OID." + oidString; } } /** * Test if oid has an associated keyword in standard. */ static boolean hasKeyword(ObjectIdentifier oid, int standard) { AVAKeyword ak = (AVAKeyword)oidMap.get(oid); if (ak == null) { return false; } return ak.isCompliant(standard); } static { oidMap = new HashMap(); keywordMap = new HashMap(); // NOTE if multiple keywords are available for one OID, order // is significant!! Preferred *LAST*. new AVAKeyword("CN", X500Name.commonName_oid, true, true); new AVAKeyword("C", X500Name.countryName_oid, true, true); new AVAKeyword("L", X500Name.localityName_oid, true, true); new AVAKeyword("S", X500Name.stateName_oid, false, false); new AVAKeyword("ST", X500Name.stateName_oid, true, true); new AVAKeyword("O", X500Name.orgName_oid, true, true); new AVAKeyword("OU", X500Name.orgUnitName_oid, true, true); new AVAKeyword("T", X500Name.title_oid, false, false); new AVAKeyword("IP", X500Name.ipAddress_oid, false, false); new AVAKeyword("STREET", X500Name.streetAddress_oid,true, true); new AVAKeyword("DC", X500Name.DOMAIN_COMPONENT_OID, false, true); new AVAKeyword("DNQUALIFIER", X500Name.DNQUALIFIER_OID, false, false); new AVAKeyword("DNQ", X500Name.DNQUALIFIER_OID, false, false); new AVAKeyword("SURNAME", X500Name.SURNAME_OID, false, false); new AVAKeyword("GIVENNAME", X500Name.GIVENNAME_OID, false, false); new AVAKeyword("INITIALS", X500Name.INITIALS_OID, false, false); new AVAKeyword("GENERATION", X500Name.GENERATIONQUALIFIER_OID, false, false); new AVAKeyword("EMAIL", PKCS9Attribute.EMAIL_ADDRESS_OID, false, false); new AVAKeyword("EMAILADDRESS", PKCS9Attribute.EMAIL_ADDRESS_OID, false, false); new AVAKeyword("UID", X500Name.userid_oid, false, true); new AVAKeyword("SERIALNUMBER", X500Name.SERIALNUMBER_OID, false, false); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -