/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* X.500 Attribute-Value-Assertion (AVA): an attribute, as identified by
* some attribute ID, has some particular value. Values are as a rule ASN.1
* printable strings. A conventional set of type IDs is recognized when
* parsing (and generating) RFC 1779 or RFC 2253 syntax strings.
*
* <P>AVAs are components of X.500 relative names. Think of them as being
* individual fields of a database record. The attribute ID is how you
* identify the field, and the value is part of a particular record.
* <p>
* Note that instances of this class are immutable.
*
* @see X500Name
* @see RDN
*
*
* @author David Brownell
* @author Amit Kapoor
* @author Hemma Prafullchandra
*/
// See CR 6391482: if enabled this flag preserves the old but incorrect
// PrintableString encoding for DomainComponent. It may need to be set to
// avoid breaking preexisting certificates generated with sun.security APIs.
private static final boolean PRESERVE_OLD_DC_ENCODING =
("com.sun.security.preserveOldDCEncoding"));
/**
* DEFAULT format allows both RFC1779 and RFC2253 syntax and
* additional keywords.
*/
/**
* RFC1779 specifies format according to RFC1779.
*/
/**
* RFC2253 specifies format according to RFC2253.
*/
// currently not private, accessed directly from RDN
/*
* If the value has any of these characters in it, it must be quoted.
* Backslash and quote characters must also be individually escaped.
* Leading and trailing spaces, also multiple internal spaces, also
* call for quoting the whole string.
*/
/*
* In RFC2253, if the value has any of these characters in it, it
* must be quoted by a preceding \.
*/
/*
* includes special chars from RFC1779 and RFC2253, as well as ' '
*/
/*
* Values that aren't printable strings are emitted as BER-encoded
* hex data.
*/
throw new NullPointerException();
}
}
/**
* Parse an RFC 1779 or RFC 2253 style AVA string: CN=fee fie foe fum
* or perhaps with quotes. Not all defined AVA tags are supported;
* of current note are X.400 related ones (PRMD, ADMD, etc).
*
* This terminates at unescaped AVA separators ("+") or RDN
* separators (",", ";"), or DN terminators (">"), and removes
* cosmetic whitespace at the end of values.
*/
}
/**
* Parse an RFC 1779 or RFC 2253 style AVA string: CN=fee fie foe fum
* or perhaps with quotes. Additional keywords can be specified in the
*
* This terminates at unescaped AVA separators ("+") or RDN
* separators (",", ";"), or DN terminators (">"), and removes
* cosmetic whitespace at the end of values.
*/
}
/**
* Parse an AVA string formatted according to format.
*
* XXX format RFC1779 should only allow RFC1779 syntax but is
* actually DEFAULT with RFC1779 keywords.
*/
}
/**
* Parse an AVA string formatted according to format.
*
* XXX format RFC1779 should only allow RFC1779 syntax but is
* actually DEFAULT with RFC1779 keywords.
*
* @param in Reader containing AVA String
* @param format parsing format
* @param keywordMap a Map where a keyword String maps to a corresponding
* OID String. Each AVA keyword will be mapped to the corresponding OID.
* If an entry does not exist, it will fallback to the builtin
* @throws IOException if the AVA String is not valid in the specified
* standard or an OID String from the keywordMap is improperly formatted
*/
throws IOException {
// assume format is one of DEFAULT, RFC1779, RFC2253
int c;
/*
* First get the keyword indicating the attribute's type,
* and map it to the appropriate OID.
*/
while (true) {
if (c == '=') {
break;
}
}
/*
* Now parse the value. "#hex", a quoted string, or a string
* terminated by "+", ",", ";", ">". Whitespace before or after
* the value is stripped away unless format is RFC2253.
*/
// read next character
if (c == ' ') {
throw new IOException("Incorrect AVA RFC2253 format - " +
"leading space must be escaped");
}
} else {
// read next character skipping whitespace
do {
} while ((c == ' ') || (c == '\n'));
}
if (c == -1) {
// empty value
return;
}
if (c == '#') {
} else {
}
}
/**
* Get the ObjectIdentifier of this AVA.
*/
return oid;
}
/**
* Get the value of this AVA as a DerValue.
*/
return value;
}
/**
* Get the value of this AVA as a String.
*
* @exception RuntimeException if we could not obtain the string form
* (should not occur)
*/
try {
if (s == null) {
throw new RuntimeException("AVA string is null");
}
return s;
} catch (IOException e) {
// should not occur
throw new RuntimeException("AVA error: " + e, e);
}
}
int c;
byte b = 0;
int cNdx = 0;
while (true) {
if (isTerminator(c, format)) {
break;
}
if (cVal == -1) {
throw new IOException("AVA parse, invalid hex " +
"digit: "+ (char)c);
}
b = (byte)((b * 16) + (byte)(cVal));
} else {
b = (byte)(cVal);
}
cNdx++;
}
// throw exception if no hex digits
if (cNdx == 0) {
throw new IOException("AVA parse, zero hex digits");
}
// throw exception if odd number of hex digits
throw new IOException("AVA parse, odd number of hex digits");
}
}
// RFC1779 specifies that an entire RDN may be enclosed in double
// quotes. In this case the syntax is any sequence of
// backslash-specialChar, backslash-backslash,
// backslash-doublequote, or character other than backslash or
// doublequote.
boolean isPrintableString = true;
while (c != '"') {
if (c == '\\') {
// check for embedded hex pairs
// always encode AVAs with embedded hex as UTF8
isPrintableString = false;
// append consecutive embedded hex
// as single string later
continue;
}
if (c != '\\' && c != '"' &&
throw new IOException
("Invalid escaped character in AVA: " +
(char)c);
}
}
// add embedded hex bytes before next char
embeddedHex.clear();
}
// check for non-PrintableString chars
}
// add trailing embedded hex bytes
embeddedHex.clear();
}
do {
} while ((c == '\n') || (c == ' '));
if (c != -1) {
throw new IOException("AVA had characters other than "
+ "whitespace after terminating quote");
}
// encode as PrintableString unless value contains
// non-PrintableString chars
PRESERVE_OLD_DC_ENCODING == false)) {
// EmailAddress and DomainComponent must be IA5String
} else if (isPrintableString) {
} else {
}
}
boolean isPrintableString = true;
boolean escape = false;
boolean leadingChar = true;
int spaceCount = 0;
do {
escape = false;
if (c == '\\') {
escape = true;
// check for embedded hex pairs
// always encode AVAs with embedded hex as UTF8
isPrintableString = false;
// append consecutive embedded hex
// as single string later
leadingChar = false;
continue;
}
// check if character was improperly escaped
c != '\\' && c != '\"')) {
throw new IOException
("Invalid escaped character in AVA: '" +
(char)c + "'");
if (c == ' ') {
throw new IOException
("Invalid escaped space character " +
"in AVA. Only a leading or trailing " +
"space character can be escaped.");
}
} else if (c == '#') {
// only leading '#' can be escaped
if (!leadingChar) {
throw new IOException
("Invalid escaped '#' character in AVA. " +
"Only a leading '#' can be escaped.");
}
throw new IOException
("Invalid escaped character in AVA: '" +
(char)c + "'");
}
}
} else {
// check if character should have been escaped
throw new IOException
("Character '" + (char)c +
"' in AVA appears without escape");
}
}
}
// add embedded hex bytes before next char
// add space(s) before embedded hex bytes
for (int i = 0; i < spaceCount; i++) {
}
spaceCount = 0;
embeddedHex.clear();
}
// check for non-PrintableString chars
if (c == ' ' && escape == false) {
// do not add non-escaped spaces yet
// (non-escaped trailing spaces are ignored)
spaceCount++;
} else {
// add space(s)
for (int i = 0; i < spaceCount; i++) {
}
spaceCount = 0;
}
leadingChar = false;
} while (isTerminator(c, format) == false);
throw new IOException("Incorrect AVA RFC2253 format - " +
"trailing space must be escaped");
}
// add trailing embedded hex bytes
embeddedHex.clear();
}
// encode as PrintableString unless value contains
// non-PrintableString chars
PRESERVE_OLD_DC_ENCODING == false)) {
// EmailAddress and DomainComponent must be IA5String
} else if (isPrintableString) {
} else {
}
}
throws IOException {
"escaped hex value must include two valid digits");
} else {
throw new IOException
("escaped hex value must include two valid digits");
}
}
return null;
}
throws IOException {
byte[] hexBytes = new byte[n];
for (int i = 0; i < n; i++) {
}
}
switch (ch) {
case -1:
case '+':
case ',':
return true;
case ';':
case '>':
default:
return false;
}
}
if (c == -1) {
throw new IOException(errMsg);
}
return c;
}
boolean trailing = false;
if (!in.markSupported()) {
// oh well
return true;
} else {
// make readAheadLimit huge -
// in practice, AVA was passed a StringReader from X500Name,
// and StringReader ignores readAheadLimit anyways
while (true) {
if (nextChar == -1) {
trailing = true;
break;
} else if (nextChar == ' ') {
continue;
} else if (nextChar == '\\') {
if (followingChar != ' ') {
trailing = false;
break;
}
} else {
trailing = false;
break;
}
}
return trailing;
}
}
// Individual attribute value assertions are SEQUENCE of two values.
// That'd be a "struct" outside of ASN.1.
throw new IOException("AVA not a sequence");
}
throw new IOException("AVA, extra bytes = "
}
}
this(in.getDerValue());
}
if (this == obj) {
return true;
}
return false;
}
return this.toRFC2253CanonicalString().equals
}
/**
* Returns a hashcode for this AVA.
*
* @return a hashcode for this AVA.
*/
public int hashCode() {
return toRFC2253CanonicalString().hashCode();
}
/*
* AVAs are encoded as a SEQUENCE of two elements.
*/
}
/**
* DER encode this object onto an output stream.
* Implements the <code>DerEncoder</code> interface.
*
* @param out
* the output stream on which to write the DER encoding.
*
* @exception IOException on encoding error.
*/
}
}
/**
* Returns a printable form of this attribute, using RFC 1779
*/
return toKeywordValueString
}
/**
* Returns a printable form of this attribute, using RFC 1779
* emits standardised keywords.
*/
}
/**
* Returns a printable form of this attribute, using RFC 1779
* emits standardised keywords, as well as keywords contained in the
*/
}
/**
* Returns a printable form of this attribute, using RFC 2253
* emits standardised keywords.
*/
}
/**
* Returns a printable form of this attribute, using RFC 2253
* emits standardised keywords, as well as keywords contained in the
*/
/*
* 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
* section 2.4.
*/
/*
* Section 2.4: Converting an AttributeValue from ASN.1 to a String.
* If the AttributeValue is of a type which does not have a string
* representation defined for it, then it is simply encoded as an
* octothorpe character ('#' ASCII 35) followed by the hexadecimal
* representation of each of the bytes of the BER encoding of the X.500
* AttributeValue. This form SHOULD be used if the AttributeType is of
* the dotted-decimal form.
*/
!isDerString(value, false))
{
try {
} catch (IOException ie) {
throw new IllegalArgumentException("DER Value conversion");
}
byte b = data[j];
}
} 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().
*/
try {
} 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 ";"
*
* Implementations MAY escape other characters.
*
* NOTE: this implementation also recognizes "=" and "#" as
* characters which need escaping, and null which is escaped as
* '\00' (see RFC 4514).
*
* 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.
*/
if (DerValue.isPrintableStringChar(c) ||
// escape escapees
}
} else if (c == '\u0000') {
// escape null character
// embed non-printable/non-escaped char
// as escaped hex pairs for debugging
byte[] valueBytes = null;
try {
} catch (IOException ie) {
throw new IllegalArgumentException
("DER Value conversion");
}
}
} else {
// append non-printable/non-escaped char
}
}
sbuffer = new StringBuilder();
// Find leading and trailing whitespace.
int lead; // index of first char that is not leading whitespace
break;
}
}
int trail; // index of last char that is not trailing whitespace
break;
}
}
// escape leading and trailing whitespace
char c = chars[i];
}
}
}
return typeAndValue.toString();
}
/*
* 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
* section 2.4.
*/
/*
* Section 2.4: Converting an AttributeValue from ASN.1 to a String.
* If the AttributeValue is of a type which does not have a string
* representation defined for it, then it is simply encoded as an
* octothorpe character ('#' ASCII 35) followed by the hexadecimal
* representation of each of the bytes of the BER encoding of the X.500
* AttributeValue. This form SHOULD be used if the AttributeType is of
* the dotted-decimal form.
*/
!isDerString(value, true))
{
try {
} catch (IOException ie) {
throw new IllegalArgumentException("DER Value conversion");
}
byte b = data[j];
}
} 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().
*/
try {
} 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.
*/
boolean previousWhite = false;
if (DerValue.isPrintableStringChar(c) ||
(i == 0 && c == '#')) {
// escape leading '#' and escapees
}
// convert multiple whitespace to single whitespace
if (!Character.isWhitespace(c)) {
previousWhite = false;
} else {
if (previousWhite == false) {
// add single whitespace
previousWhite = true;
} else {
// ignore subsequent consecutive whitespace
continue;
}
}
// embed non-printable/non-escaped char
// as escaped hex pairs for debugging
previousWhite = false;
byte valueBytes[] = null;
try {
} catch (IOException ie) {
throw new IllegalArgumentException
("DER Value conversion");
}
}
} else {
// append non-printable/non-escaped char
previousWhite = false;
}
}
// remove leading and trailing whitespace from value
}
}
/*
* Return true if DerValue can be represented as a String.
*/
if (canonical) {
case DerValue.tag_PrintableString:
case DerValue.tag_UTF8String:
return true;
default:
return false;
}
} else {
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() {
}
/*
* Construct the value with as little copying and garbage
* production as practical. First the keyword (mandatory),
* then the equals sign, finally the value.
*/
try {
// 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
}
} else {
boolean quoteNeeded = false;
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.
*/
boolean alreadyQuoted =
for (int i = 0; i < length; i++) {
continue;
}
if (DerValue.isPrintableStringChar(c) ||
// quote if leading whitespace or special chars
if (!quoteNeeded &&
((i == 0 && (c == ' ' || c == '\n')) ||
quoteNeeded = true;
}
// quote if multiple internal whitespace
if (!(c == ' ' || c == '\n')) {
// escape '"' and '\'
if (c == '"' || c == '\\') {
}
previousWhite = false;
} else {
if (!quoteNeeded && previousWhite) {
quoteNeeded = true;
}
previousWhite = true;
}
// embed non-printable/non-escaped char
// as escaped hex pairs for debugging
previousWhite = false;
// embed escaped hex pairs
byte[] valueBytes =
}
} else {
// append non-printable/non-escaped char
previousWhite = false;
}
}
// quote if trailing whitespace
quoteNeeded = true;
}
}
// Emit the string ... quote it if needed
// if string is already quoted, don't re-quote
if (!alreadyQuoted && quoteNeeded) {
} else {
}
}
} catch (IOException e) {
throw new IllegalArgumentException("DER Value conversion");
}
}
}
/**
* 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 {
boolean rfc1779Compliant, boolean rfc2253Compliant) {
this.rfc1779Compliant = rfc1779Compliant;
this.rfc2253Compliant = rfc2253Compliant;
// register it
}
switch (standard) {
return rfc1779Compliant;
return rfc2253Compliant;
return true;
default:
// should not occur, internal error
}
}
/**
* 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
*/
throws IOException {
return getOID
}
/**
* Get an object identifier representing the specified keyword (or
* string encoded object identifier) in the given standard.
*
* @param keywordMap a Map where a keyword String maps to a corresponding
* OID String. Each AVA keyword will be mapped to the corresponding OID.
* If an entry does not exist, it will fallback to the builtin
* @throws IOException If the keyword is not valid in the specified standard
* or the OID String to which a keyword maps to is improperly formatted.
*/
throws IOException {
throw new IOException("Invalid leading or trailing space " +
}
} else {
}
// check user-specified keyword map first, then fallback to built-in
// map
}
} else {
return new ObjectIdentifier(oidString);
}
// no keyword found or not standard compliant, check if OID string
// RFC1779 requires, DEFAULT allows OID. prefix
}
}
}
boolean number = false;
number = true;
}
}
if (number == false) {
}
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.
*/
return getKeyword
}
/**
* Get a keyword for the given ObjectIdentifier according to standard.
* Checks the extraOidMap for a keyword first, then falls back to the
* is encoded as a String.
*/
// check extraOidMap first, then fallback to built-in map
if (keywordString == null) {
}
} else {
throw new IllegalArgumentException("keyword cannot be empty");
}
if (c < 65 || c > 122 || (c > 90 && c < 97)) {
throw new IllegalArgumentException
("keyword does not start with letter");
}
c = keywordString.charAt(i);
if ((c < 65 || c > 122 || (c > 90 && c < 97)) &&
(c < 48 || c > 57) && c != '_') {
throw new IllegalArgumentException
("keyword character is not a letter, digit, or underscore");
}
}
return keywordString;
}
// no compliant keyword, use OID
return oidString;
} else {
return "OID." + oidString;
}
}
/**
* Test if oid has an associated keyword in standard.
*/
return false;
}
}
static {
// NOTE if multiple keywords are available for one OID, order
// is significant!! Preferred *LAST*.
false, true);
false, false);
false, false);
}
}