< prev index next >

src/java.base/share/classes/sun/security/x509/AVA.java

Print this page
*** 106,16 ***
       * RFC 4514.
       */
      private static final String specialCharsDefault = ",=\n+<>#;\\\" ";
      private static final String escapedDefault = ",+<>;\"";
  
-     /*
-      * Values that aren't printable strings are emitted as BER-encoded
-      * hex data.
-      */
-     private static final String hexDigits = "0123456789ABCDEF";
- 
      public AVA(ObjectIdentifier type, DerValue val) {
          if ((type == null) || (val == null)) {
              throw new NullPointerException();
          }
          oid = type;
--- 106,10 ---

*** 267,13 ***
  
              if (isTerminator(c, format)) {
                  break;
              }
  
!             int cVal = hexDigits.indexOf(Character.toUpperCase((char)c));
  
!             if (cVal == -1) {
                  throw new IOException("AVA parse, invalid hex " +
                                                "digit: "+ (char)c);
              }
  
              if ((cNdx % 2) == 1) {
--- 261,13 ---
  
              if (isTerminator(c, format)) {
                  break;
              }
  
!             int cVal = Hex.decoder().fromHex(c);
  
!             if (cVal < 0) {
                  throw new IOException("AVA parse, invalid hex " +
                                                "digit: "+ (char)c);
              }
  
              if ((cNdx % 2) == 1) {

*** 508,18 ***
          }
      }
  
      private static Byte getEmbeddedHexPair(int c1, Reader in)
          throws IOException {
! 
!         if (hexDigits.indexOf(Character.toUpperCase((char)c1)) >= 0) {
              int c2 = readChar(in, "unexpected EOF - " +
                          "escaped hex value must include two valid digits");
  
!             if (hexDigits.indexOf(Character.toUpperCase((char)c2)) >= 0) {
!                 int hi = Character.digit((char)c1, 16);
-                 int lo = Character.digit((char)c2, 16);
                  return (byte)((hi<<4) + lo);
              } else {
                  throw new IOException
                          ("escaped hex value must include two valid digits");
              }
--- 502,18 ---
          }
      }
  
      private static Byte getEmbeddedHexPair(int c1, Reader in)
          throws IOException {
!         Hex.Decoder dec = Hex.decoder();
!         int hi = dec.fromHex(c1);
+         if (hi >= 0) {
              int c2 = readChar(in, "unexpected EOF - " +
                          "escaped hex value must include two valid digits");
  
!             int lo = dec.fromHex(c2);
!             if (lo >= 0) {
                  return (byte)((hi<<4) + lo);
              } else {
                  throw new IOException
                          ("escaped hex value must include two valid digits");
              }

*** 705,10 ***
--- 699,11 ---
       * syntax for individual attribute/value assertions. It
       * emits standardised keywords, as well as keywords contained in the
       * OID/keyword map.
       */
      public String toRFC2253String(Map<String, String> oidMap) {
+         Hex.Encoder enc = Hex.encoder();
          /*
           * Section 2.3: The AttributeTypeAndValue is encoded as the string
           * representation of the AttributeType, followed by an equals character
           * ('=' ASCII 61), followed by the string representation of the
           * AttributeValue. The encoding of the AttributeValue is given in

*** 735,15 ***
                  data = value.toByteArray();
              } 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.
--- 730,11 ---
                  data = value.toByteArray();
              } catch (IOException ie) {
                  throw new IllegalArgumentException("DER Value conversion");
              }
              typeAndValue.append('#');
!             enc.encode(typeAndValue, data);
          } 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.

*** 806,16 ***
                      // embed non-printable/non-escaped char
                      // as escaped hex pairs for debugging
                      byte[] valueBytes = Character.toString(c).getBytes(UTF_8);
                      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
                      sbuffer.append(c);
--- 797,11 ---
                      // embed non-printable/non-escaped char
                      // as escaped hex pairs for debugging
                      byte[] valueBytes = Character.toString(c).getBytes(UTF_8);
                      for (int j = 0; j < valueBytes.length; j++) {
                          sbuffer.append('\\');
!                         enc.encodeHexPair(sbuffer, valueBytes[j]);
                      }
                  } else {
  
                      // append non-printable/non-escaped char
                      sbuffer.append(c);

*** 851,10 ***
--- 837,11 ---
          }
          return typeAndValue.toString();
      }
  
      public String toRFC2253CanonicalString() {
+         Hex.Encoder enc = Hex.encoder();
          /*
           * Section 2.3: The AttributeTypeAndValue is encoded as the string
           * representation of the AttributeType, followed by an equals character
           * ('=' ASCII 61), followed by the string representation of the
           * AttributeValue. The encoding of the AttributeValue is given in

*** 882,15 ***
                  data = value.toByteArray();
              } 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.
--- 869,11 ---
                  data = value.toByteArray();
              } catch (IOException ie) {
                  throw new IllegalArgumentException("DER Value conversion");
              }
              typeAndValue.append('#');
!             enc.encode(typeAndValue, data);
          } 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.

*** 953,23 ***
                              continue;
                          }
                      }
  
                  } else if (debug != null && Debug.isOn("ava")) {
- 
                      // embed non-printable/non-escaped char
                      // as escaped hex pairs for debugging
  
                      previousWhite = false;
  
                      byte[] valueBytes = Character.toString(c).getBytes(UTF_8);
                      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
  
--- 936,19 ---
                              continue;
                          }
                      }
  
                  } else if (debug != null && Debug.isOn("ava")) {
                      // embed non-printable/non-escaped char
                      // as escaped hex pairs for debugging
  
                      previousWhite = false;
  
                      byte[] valueBytes = Character.toString(c).getBytes(UTF_8);
                      for (int j = 0; j < valueBytes.length; j++) {
                          sbuffer.append('\\');
!                         enc.encodeHexPair(sbuffer, valueBytes[j]);
                      }
                  } else {
  
                      // append non-printable/non-escaped char
  

*** 1023,11 ***
           * Construct the value with as little copying and garbage
           * production as practical.  First the keyword (mandatory),
           * then the equals sign, finally the value.
           */
          StringBuilder   retval = new StringBuilder(40);
! 
          retval.append(keyword);
          retval.append('=');
  
          try {
              String valStr = value.getAsString();
--- 1002,11 ---
           * Construct the value with as little copying and garbage
           * production as practical.  First the keyword (mandatory),
           * then the equals sign, finally the value.
           */
          StringBuilder   retval = new StringBuilder(40);
!         Hex.Encoder enc = Hex.encoder();
          retval.append(keyword);
          retval.append('=');
  
          try {
              String valStr = value.getAsString();

*** 1040,15 ***
                  // 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;
                  StringBuilder sbuffer = new StringBuilder();
                  boolean previousWhite = false;
--- 1019,11 ---
                  // when the value is not a string type
  
                  byte[] data = value.toByteArray();
  
                  retval.append('#');
!                 enc.encode(retval, data);
              } else {
  
                  boolean quoteNeeded = false;
                  StringBuilder sbuffer = new StringBuilder();
                  boolean previousWhite = false;

*** 1106,16 ***
                          // embed escaped hex pairs
                          byte[] valueBytes =
                                  Character.toString(c).getBytes(UTF_8);
                          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
  
--- 1081,11 ---
                          // embed escaped hex pairs
                          byte[] valueBytes =
                                  Character.toString(c).getBytes(UTF_8);
                          for (int j = 0; j < valueBytes.length; j++) {
                              sbuffer.append('\\');
!                             enc.encodeHexPair(sbuffer, valueBytes[j]);
                          }
                      } else {
  
                          // append non-printable/non-escaped char
  
< prev index next >