0N/A/*
4397N/A * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.x509;
0N/A
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.Reader;
0N/Aimport java.security.AccessController;
0N/Aimport java.text.Normalizer;
0N/Aimport java.util.*;
0N/A
0N/Aimport sun.security.action.GetBooleanAction;
0N/Aimport sun.security.util.*;
0N/Aimport sun.security.pkcs.PKCS9Attribute;
0N/A
0N/A
0N/A/**
0N/A * X.500 Attribute-Value-Assertion (AVA): an attribute, as identified by
0N/A * some attribute ID, has some particular value. Values are as a rule ASN.1
0N/A * printable strings. A conventional set of type IDs is recognized when
0N/A * parsing (and generating) RFC 1779 or RFC 2253 syntax strings.
0N/A *
0N/A * <P>AVAs are components of X.500 relative names. Think of them as being
0N/A * individual fields of a database record. The attribute ID is how you
0N/A * identify the field, and the value is part of a particular record.
0N/A * <p>
0N/A * Note that instances of this class are immutable.
0N/A *
0N/A * @see X500Name
0N/A * @see RDN
0N/A *
0N/A *
0N/A * @author David Brownell
0N/A * @author Amit Kapoor
0N/A * @author Hemma Prafullchandra
0N/A */
0N/Apublic class AVA implements DerEncoder {
0N/A
0N/A private static final Debug debug = Debug.getInstance("x509", "\t[AVA]");
0N/A // See CR 6391482: if enabled this flag preserves the old but incorrect
0N/A // PrintableString encoding for DomainComponent. It may need to be set to
0N/A // avoid breaking preexisting certificates generated with sun.security APIs.
0N/A private static final boolean PRESERVE_OLD_DC_ENCODING =
0N/A AccessController.doPrivileged(new GetBooleanAction
0N/A ("com.sun.security.preserveOldDCEncoding"));
0N/A
0N/A /**
0N/A * DEFAULT format allows both RFC1779 and RFC2253 syntax and
0N/A * additional keywords.
0N/A */
0N/A final static int DEFAULT = 1;
0N/A /**
0N/A * RFC1779 specifies format according to RFC1779.
0N/A */
0N/A final static int RFC1779 = 2;
0N/A /**
0N/A * RFC2253 specifies format according to RFC2253.
0N/A */
0N/A final static int RFC2253 = 3;
0N/A
0N/A // currently not private, accessed directly from RDN
0N/A final ObjectIdentifier oid;
0N/A final DerValue value;
0N/A
0N/A /*
0N/A * If the value has any of these characters in it, it must be quoted.
0N/A * Backslash and quote characters must also be individually escaped.
0N/A * Leading and trailing spaces, also multiple internal spaces, also
0N/A * call for quoting the whole string.
0N/A */
0N/A private static final String specialChars = ",+=\n<>#;";
0N/A
0N/A /*
0N/A * In RFC2253, if the value has any of these characters in it, it
0N/A * must be quoted by a preceding \.
0N/A */
0N/A private static final String specialChars2253 = ",+\"\\<>;";
0N/A
0N/A /*
0N/A * includes special chars from RFC1779 and RFC2253, as well as ' '
0N/A */
0N/A private static final String specialCharsAll = ",=\n+<>#;\\\" ";
0N/A
0N/A /*
0N/A * Values that aren't printable strings are emitted as BER-encoded
0N/A * hex data.
0N/A */
0N/A private static final String hexDigits = "0123456789ABCDEF";
0N/A
0N/A public AVA(ObjectIdentifier type, DerValue val) {
0N/A if ((type == null) || (val == null)) {
0N/A throw new NullPointerException();
0N/A }
0N/A oid = type;
0N/A value = val;
0N/A }
0N/A
0N/A /**
0N/A * Parse an RFC 1779 or RFC 2253 style AVA string: CN=fee fie foe fum
0N/A * or perhaps with quotes. Not all defined AVA tags are supported;
0N/A * of current note are X.400 related ones (PRMD, ADMD, etc).
0N/A *
0N/A * This terminates at unescaped AVA separators ("+") or RDN
0N/A * separators (",", ";"), or DN terminators (">"), and removes
0N/A * cosmetic whitespace at the end of values.
0N/A */
0N/A AVA(Reader in) throws IOException {
0N/A this(in, DEFAULT);
0N/A }
0N/A
0N/A /**
0N/A * Parse an RFC 1779 or RFC 2253 style AVA string: CN=fee fie foe fum
0N/A * or perhaps with quotes. Additional keywords can be specified in the
0N/A * keyword/OID map.
0N/A *
0N/A * This terminates at unescaped AVA separators ("+") or RDN
0N/A * separators (",", ";"), or DN terminators (">"), and removes
0N/A * cosmetic whitespace at the end of values.
0N/A */
0N/A AVA(Reader in, Map<String, String> keywordMap) throws IOException {
0N/A this(in, DEFAULT, keywordMap);
0N/A }
0N/A
0N/A /**
0N/A * Parse an AVA string formatted according to format.
0N/A *
0N/A * XXX format RFC1779 should only allow RFC1779 syntax but is
0N/A * actually DEFAULT with RFC1779 keywords.
0N/A */
0N/A AVA(Reader in, int format) throws IOException {
0N/A this(in, format, Collections.<String, String>emptyMap());
0N/A }
0N/A
0N/A /**
0N/A * Parse an AVA string formatted according to format.
0N/A *
0N/A * XXX format RFC1779 should only allow RFC1779 syntax but is
0N/A * actually DEFAULT with RFC1779 keywords.
0N/A *
0N/A * @param in Reader containing AVA String
0N/A * @param format parsing format
0N/A * @param keywordMap a Map where a keyword String maps to a corresponding
0N/A * OID String. Each AVA keyword will be mapped to the corresponding OID.
0N/A * If an entry does not exist, it will fallback to the builtin
0N/A * keyword/OID mapping.
0N/A * @throws IOException if the AVA String is not valid in the specified
0N/A * standard or an OID String from the keywordMap is improperly formatted
0N/A */
0N/A AVA(Reader in, int format, Map<String, String> keywordMap)
0N/A throws IOException {
0N/A // assume format is one of DEFAULT, RFC1779, RFC2253
0N/A
0N/A StringBuilder temp = new StringBuilder();
0N/A int c;
0N/A
0N/A /*
0N/A * First get the keyword indicating the attribute's type,
0N/A * and map it to the appropriate OID.
0N/A */
0N/A while (true) {
0N/A c = readChar(in, "Incorrect AVA format");
0N/A if (c == '=') {
0N/A break;
0N/A }
0N/A temp.append((char)c);
0N/A }
0N/A
0N/A oid = AVAKeyword.getOID(temp.toString(), format, keywordMap);
0N/A
0N/A /*
0N/A * Now parse the value. "#hex", a quoted string, or a string
0N/A * terminated by "+", ",", ";", ">". Whitespace before or after
0N/A * the value is stripped away unless format is RFC2253.
0N/A */
0N/A temp.setLength(0);
0N/A if (format == RFC2253) {
0N/A // read next character
0N/A c = in.read();
0N/A if (c == ' ') {
0N/A throw new IOException("Incorrect AVA RFC2253 format - " +
0N/A "leading space must be escaped");
0N/A }
0N/A } else {
0N/A // read next character skipping whitespace
0N/A do {
0N/A c = in.read();
0N/A } while ((c == ' ') || (c == '\n'));
0N/A }
0N/A if (c == -1) {
0N/A // empty value
0N/A value = new DerValue("");
0N/A return;
0N/A }
0N/A
0N/A if (c == '#') {
0N/A value = parseHexString(in, format);
0N/A } else if ((c == '"') && (format != RFC2253)) {
0N/A value = parseQuotedString(in, temp);
0N/A } else {
0N/A value = parseString(in, c, format, temp);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the ObjectIdentifier of this AVA.
0N/A */
0N/A public ObjectIdentifier getObjectIdentifier() {
0N/A return oid;
0N/A }
0N/A
0N/A /**
0N/A * Get the value of this AVA as a DerValue.
0N/A */
0N/A public DerValue getDerValue() {
0N/A return value;
0N/A }
0N/A
0N/A /**
0N/A * Get the value of this AVA as a String.
0N/A *
0N/A * @exception RuntimeException if we could not obtain the string form
0N/A * (should not occur)
0N/A */
0N/A public String getValueString() {
0N/A try {
0N/A String s = value.getAsString();
0N/A if (s == null) {
0N/A throw new RuntimeException("AVA string is null");
0N/A }
0N/A return s;
0N/A } catch (IOException e) {
0N/A // should not occur
0N/A throw new RuntimeException("AVA error: " + e, e);
0N/A }
0N/A }
0N/A
0N/A private static DerValue parseHexString
0N/A (Reader in, int format) throws IOException {
0N/A
0N/A int c;
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A byte b = 0;
0N/A int cNdx = 0;
0N/A while (true) {
0N/A c = in.read();
0N/A
0N/A if (isTerminator(c, format)) {
0N/A break;
0N/A }
0N/A
0N/A int cVal = hexDigits.indexOf(Character.toUpperCase((char)c));
0N/A
0N/A if (cVal == -1) {
0N/A throw new IOException("AVA parse, invalid hex " +
0N/A "digit: "+ (char)c);
0N/A }
0N/A
0N/A if ((cNdx % 2) == 1) {
0N/A b = (byte)((b * 16) + (byte)(cVal));
0N/A baos.write(b);
0N/A } else {
0N/A b = (byte)(cVal);
0N/A }
0N/A cNdx++;
0N/A }
0N/A
0N/A // throw exception if no hex digits
0N/A if (cNdx == 0) {
0N/A throw new IOException("AVA parse, zero hex digits");
0N/A }
0N/A
0N/A // throw exception if odd number of hex digits
0N/A if (cNdx % 2 == 1) {
0N/A throw new IOException("AVA parse, odd number of hex digits");
0N/A }
0N/A
0N/A return new DerValue(baos.toByteArray());
0N/A }
0N/A
0N/A private DerValue parseQuotedString
0N/A (Reader in, StringBuilder temp) throws IOException {
0N/A
0N/A // RFC1779 specifies that an entire RDN may be enclosed in double
0N/A // quotes. In this case the syntax is any sequence of
0N/A // backslash-specialChar, backslash-backslash,
0N/A // backslash-doublequote, or character other than backslash or
0N/A // doublequote.
0N/A int c = readChar(in, "Quoted string did not end in quote");
0N/A
0N/A List<Byte> embeddedHex = new ArrayList<Byte>();
0N/A boolean isPrintableString = true;
0N/A while (c != '"') {
0N/A if (c == '\\') {
0N/A c = readChar(in, "Quoted string did not end in quote");
0N/A
0N/A // check for embedded hex pairs
0N/A Byte hexByte = null;
0N/A if ((hexByte = getEmbeddedHexPair(c, in)) != null) {
0N/A
0N/A // always encode AVAs with embedded hex as UTF8
0N/A isPrintableString = false;
0N/A
0N/A // append consecutive embedded hex
0N/A // as single string later
0N/A embeddedHex.add(hexByte);
0N/A c = in.read();
0N/A continue;
0N/A }
0N/A
0N/A if (c != '\\' && c != '"' &&
0N/A specialChars.indexOf((char)c) < 0) {
0N/A throw new IOException
0N/A ("Invalid escaped character in AVA: " +
0N/A (char)c);
0N/A }
0N/A }
0N/A
0N/A // add embedded hex bytes before next char
0N/A if (embeddedHex.size() > 0) {
0N/A String hexString = getEmbeddedHexString(embeddedHex);
0N/A temp.append(hexString);
0N/A embeddedHex.clear();
0N/A }
0N/A
0N/A // check for non-PrintableString chars
0N/A isPrintableString &= DerValue.isPrintableStringChar((char)c);
0N/A temp.append((char)c);
0N/A c = readChar(in, "Quoted string did not end in quote");
0N/A }
0N/A
0N/A // add trailing embedded hex bytes
0N/A if (embeddedHex.size() > 0) {
0N/A String hexString = getEmbeddedHexString(embeddedHex);
0N/A temp.append(hexString);
0N/A embeddedHex.clear();
0N/A }
0N/A
0N/A do {
0N/A c = in.read();
0N/A } while ((c == '\n') || (c == ' '));
0N/A if (c != -1) {
0N/A throw new IOException("AVA had characters other than "
0N/A + "whitespace after terminating quote");
0N/A }
0N/A
0N/A // encode as PrintableString unless value contains
0N/A // non-PrintableString chars
0N/A if (this.oid.equals(PKCS9Attribute.EMAIL_ADDRESS_OID) ||
0N/A (this.oid.equals(X500Name.DOMAIN_COMPONENT_OID) &&
0N/A PRESERVE_OLD_DC_ENCODING == false)) {
0N/A // EmailAddress and DomainComponent must be IA5String
0N/A return new DerValue(DerValue.tag_IA5String,
0N/A temp.toString().trim());
0N/A } else if (isPrintableString) {
0N/A return new DerValue(temp.toString().trim());
0N/A } else {
0N/A return new DerValue(DerValue.tag_UTF8String,
0N/A temp.toString().trim());
0N/A }
0N/A }
0N/A
0N/A private DerValue parseString
0N/A (Reader in, int c, int format, StringBuilder temp) throws IOException {
0N/A
0N/A List<Byte> embeddedHex = new ArrayList<Byte>();
0N/A boolean isPrintableString = true;
0N/A boolean escape = false;
0N/A boolean leadingChar = true;
0N/A int spaceCount = 0;
0N/A do {
0N/A escape = false;
0N/A if (c == '\\') {
0N/A escape = true;
0N/A c = readChar(in, "Invalid trailing backslash");
0N/A
0N/A // check for embedded hex pairs
0N/A Byte hexByte = null;
0N/A if ((hexByte = getEmbeddedHexPair(c, in)) != null) {
0N/A
0N/A // always encode AVAs with embedded hex as UTF8
0N/A isPrintableString = false;
0N/A
0N/A // append consecutive embedded hex
0N/A // as single string later
0N/A embeddedHex.add(hexByte);
0N/A c = in.read();
0N/A leadingChar = false;
0N/A continue;
0N/A }
0N/A
0N/A // check if character was improperly escaped
0N/A if ((format == DEFAULT &&
0N/A specialCharsAll.indexOf((char)c) == -1) ||
0N/A (format == RFC1779 &&
0N/A specialChars.indexOf((char)c) == -1 &&
0N/A c != '\\' && c != '\"')) {
0N/A
0N/A throw new IOException
0N/A ("Invalid escaped character in AVA: '" +
0N/A (char)c + "'");
0N/A
0N/A } else if (format == RFC2253) {
0N/A if (c == ' ') {
0N/A // only leading/trailing space can be escaped
0N/A if (!leadingChar && !trailingSpace(in)) {
0N/A throw new IOException
0N/A ("Invalid escaped space character " +
0N/A "in AVA. Only a leading or trailing " +
0N/A "space character can be escaped.");
0N/A }
0N/A } else if (c == '#') {
0N/A // only leading '#' can be escaped
0N/A if (!leadingChar) {
0N/A throw new IOException
0N/A ("Invalid escaped '#' character in AVA. " +
0N/A "Only a leading '#' can be escaped.");
0N/A }
0N/A } else if (specialChars2253.indexOf((char)c) == -1) {
0N/A throw new IOException
0N/A ("Invalid escaped character in AVA: '" +
0N/A (char)c + "'");
0N/A
0N/A }
0N/A }
0N/A
0N/A } else {
0N/A // check if character should have been escaped
0N/A if (format == RFC2253) {
0N/A if (specialChars2253.indexOf((char)c) != -1) {
0N/A throw new IOException
0N/A ("Character '" + (char)c +
0N/A "' in AVA appears without escape");
0N/A }
0N/A }
0N/A }
0N/A
0N/A // add embedded hex bytes before next char
0N/A if (embeddedHex.size() > 0) {
0N/A // add space(s) before embedded hex bytes
0N/A for (int i = 0; i < spaceCount; i++) {
0N/A temp.append(" ");
0N/A }
0N/A spaceCount = 0;
0N/A
0N/A String hexString = getEmbeddedHexString(embeddedHex);
0N/A temp.append(hexString);
0N/A embeddedHex.clear();
0N/A }
0N/A
0N/A // check for non-PrintableString chars
0N/A isPrintableString &= DerValue.isPrintableStringChar((char)c);
0N/A if (c == ' ' && escape == false) {
0N/A // do not add non-escaped spaces yet
0N/A // (non-escaped trailing spaces are ignored)
0N/A spaceCount++;
0N/A } else {
0N/A // add space(s)
0N/A for (int i = 0; i < spaceCount; i++) {
0N/A temp.append(" ");
0N/A }
0N/A spaceCount = 0;
0N/A temp.append((char)c);
0N/A }
0N/A c = in.read();
0N/A leadingChar = false;
0N/A } while (isTerminator(c, format) == false);
0N/A
0N/A if (format == RFC2253 && spaceCount > 0) {
0N/A throw new IOException("Incorrect AVA RFC2253 format - " +
0N/A "trailing space must be escaped");
0N/A }
0N/A
0N/A // add trailing embedded hex bytes
0N/A if (embeddedHex.size() > 0) {
0N/A String hexString = getEmbeddedHexString(embeddedHex);
0N/A temp.append(hexString);
0N/A embeddedHex.clear();
0N/A }
0N/A
0N/A // encode as PrintableString unless value contains
0N/A // non-PrintableString chars
0N/A if (this.oid.equals(PKCS9Attribute.EMAIL_ADDRESS_OID) ||
0N/A (this.oid.equals(X500Name.DOMAIN_COMPONENT_OID) &&
0N/A PRESERVE_OLD_DC_ENCODING == false)) {
0N/A // EmailAddress and DomainComponent must be IA5String
0N/A return new DerValue(DerValue.tag_IA5String, temp.toString());
0N/A } else if (isPrintableString) {
0N/A return new DerValue(temp.toString());
0N/A } else {
0N/A return new DerValue(DerValue.tag_UTF8String, temp.toString());
0N/A }
0N/A }
0N/A
0N/A private static Byte getEmbeddedHexPair(int c1, Reader in)
0N/A throws IOException {
0N/A
0N/A if (hexDigits.indexOf(Character.toUpperCase((char)c1)) >= 0) {
0N/A int c2 = readChar(in, "unexpected EOF - " +
0N/A "escaped hex value must include two valid digits");
0N/A
0N/A if (hexDigits.indexOf(Character.toUpperCase((char)c2)) >= 0) {
0N/A int hi = Character.digit((char)c1, 16);
0N/A int lo = Character.digit((char)c2, 16);
0N/A return new Byte((byte)((hi<<4) + lo));
0N/A } else {
0N/A throw new IOException
0N/A ("escaped hex value must include two valid digits");
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A private static String getEmbeddedHexString(List<Byte> hexList)
0N/A throws IOException {
0N/A int n = hexList.size();
0N/A byte[] hexBytes = new byte[n];
0N/A for (int i = 0; i < n; i++) {
0N/A hexBytes[i] = hexList.get(i).byteValue();
0N/A }
0N/A return new String(hexBytes, "UTF8");
0N/A }
0N/A
0N/A private static boolean isTerminator(int ch, int format) {
0N/A switch (ch) {
0N/A case -1:
0N/A case '+':
0N/A case ',':
0N/A return true;
0N/A case ';':
0N/A case '>':
0N/A return format != RFC2253;
0N/A default:
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A private static int readChar(Reader in, String errMsg) throws IOException {
0N/A int c = in.read();
0N/A if (c == -1) {
0N/A throw new IOException(errMsg);
0N/A }
0N/A return c;
0N/A }
0N/A
0N/A private static boolean trailingSpace(Reader in) throws IOException {
0N/A
0N/A boolean trailing = false;
0N/A
0N/A if (!in.markSupported()) {
0N/A // oh well
0N/A return true;
0N/A } else {
0N/A // make readAheadLimit huge -
0N/A // in practice, AVA was passed a StringReader from X500Name,
0N/A // and StringReader ignores readAheadLimit anyways
0N/A in.mark(9999);
0N/A while (true) {
0N/A int nextChar = in.read();
0N/A if (nextChar == -1) {
0N/A trailing = true;
0N/A break;
0N/A } else if (nextChar == ' ') {
0N/A continue;
0N/A } else if (nextChar == '\\') {
0N/A int followingChar = in.read();
0N/A if (followingChar != ' ') {
0N/A trailing = false;
0N/A break;
0N/A }
0N/A } else {
0N/A trailing = false;
0N/A break;
0N/A }
0N/A }
0N/A
0N/A in.reset();
0N/A return trailing;
0N/A }
0N/A }
0N/A
0N/A AVA(DerValue derval) throws IOException {
0N/A // Individual attribute value assertions are SEQUENCE of two values.
0N/A // That'd be a "struct" outside of ASN.1.
0N/A if (derval.tag != DerValue.tag_Sequence) {
0N/A throw new IOException("AVA not a sequence");
0N/A }
0N/A oid = X500Name.intern(derval.data.getOID());
0N/A value = derval.data.getDerValue();
0N/A
0N/A if (derval.data.available() != 0) {
0N/A throw new IOException("AVA, extra bytes = "
0N/A + derval.data.available());
0N/A }
0N/A }
0N/A
0N/A AVA(DerInputStream in) throws IOException {
0N/A this(in.getDerValue());
0N/A }
0N/A
0N/A public boolean equals(Object obj) {
0N/A if (this == obj) {
0N/A return true;
0N/A }
0N/A if (obj instanceof AVA == false) {
0N/A return false;
0N/A }
0N/A AVA other = (AVA)obj;
0N/A return this.toRFC2253CanonicalString().equals
0N/A (other.toRFC2253CanonicalString());
0N/A }
0N/A
0N/A /**
0N/A * Returns a hashcode for this AVA.
0N/A *
0N/A * @return a hashcode for this AVA.
0N/A */
0N/A public int hashCode() {
0N/A return toRFC2253CanonicalString().hashCode();
0N/A }
0N/A
0N/A /*
0N/A * AVAs are encoded as a SEQUENCE of two elements.
0N/A */
0N/A public void encode(DerOutputStream out) throws IOException {
0N/A derEncode(out);
0N/A }
0N/A
0N/A /**
0N/A * DER encode this object onto an output stream.
0N/A * Implements the <code>DerEncoder</code> interface.
0N/A *
0N/A * @param out
0N/A * the output stream on which to write the DER encoding.
0N/A *
0N/A * @exception IOException on encoding error.
0N/A */
0N/A public void derEncode(OutputStream out) throws IOException {
0N/A DerOutputStream tmp = new DerOutputStream();
0N/A DerOutputStream tmp2 = new DerOutputStream();
0N/A
0N/A tmp.putOID(oid);
0N/A value.encode(tmp);
0N/A tmp2.write(DerValue.tag_Sequence, tmp);
0N/A out.write(tmp2.toByteArray());
0N/A }
0N/A
0N/A private String toKeyword(int format, Map<String, String> oidMap) {
0N/A return AVAKeyword.getKeyword(oid, format, oidMap);
0N/A }
0N/A
0N/A /**
0N/A * Returns a printable form of this attribute, using RFC 1779
0N/A * syntax for individual attribute/value assertions.
0N/A */
0N/A public String toString() {
0N/A return toKeywordValueString
0N/A (toKeyword(DEFAULT, Collections.<String, String>emptyMap()));
0N/A }
0N/A
0N/A /**
0N/A * Returns a printable form of this attribute, using RFC 1779
0N/A * syntax for individual attribute/value assertions. It only
0N/A * emits standardised keywords.
0N/A */
0N/A public String toRFC1779String() {
0N/A return toRFC1779String(Collections.<String, String>emptyMap());
0N/A }
0N/A
0N/A /**
0N/A * Returns a printable form of this attribute, using RFC 1779
0N/A * syntax for individual attribute/value assertions. It
0N/A * emits standardised keywords, as well as keywords contained in the
0N/A * OID/keyword map.
0N/A */
0N/A public String toRFC1779String(Map<String, String> oidMap) {
0N/A return toKeywordValueString(toKeyword(RFC1779, oidMap));
0N/A }
0N/A
0N/A /**
0N/A * Returns a printable form of this attribute, using RFC 2253
0N/A * syntax for individual attribute/value assertions. It only
0N/A * emits standardised keywords.
0N/A */
0N/A public String toRFC2253String() {
0N/A return toRFC2253String(Collections.<String, String>emptyMap());
0N/A }
0N/A
0N/A /**
0N/A * Returns a printable form of this attribute, using RFC 2253
0N/A * syntax for individual attribute/value assertions. It
0N/A * emits standardised keywords, as well as keywords contained in the
0N/A * OID/keyword map.
0N/A */
0N/A public String toRFC2253String(Map<String, String> oidMap) {
0N/A /*
0N/A * Section 2.3: The AttributeTypeAndValue is encoded as the string
0N/A * representation of the AttributeType, followed by an equals character
0N/A * ('=' ASCII 61), followed by the string representation of the
0N/A * AttributeValue. The encoding of the AttributeValue is given in
0N/A * section 2.4.
0N/A */
0N/A StringBuilder typeAndValue = new StringBuilder(100);
0N/A typeAndValue.append(toKeyword(RFC2253, oidMap));
0N/A typeAndValue.append('=');
0N/A
0N/A /*
0N/A * Section 2.4: Converting an AttributeValue from ASN.1 to a String.
0N/A * If the AttributeValue is of a type which does not have a string
0N/A * representation defined for it, then it is simply encoded as an
0N/A * octothorpe character ('#' ASCII 35) followed by the hexadecimal
0N/A * representation of each of the bytes of the BER encoding of the X.500
0N/A * AttributeValue. This form SHOULD be used if the AttributeType is of
0N/A * the dotted-decimal form.
0N/A */
0N/A if ((typeAndValue.charAt(0) >= '0' && typeAndValue.charAt(0) <= '9') ||
0N/A !isDerString(value, false))
0N/A {
0N/A byte[] data = null;
0N/A try {
0N/A data = value.toByteArray();
0N/A } catch (IOException ie) {
0N/A throw new IllegalArgumentException("DER Value conversion");
0N/A }
0N/A typeAndValue.append('#');
0N/A for (int j = 0; j < data.length; j++) {
0N/A byte b = data[j];
0N/A typeAndValue.append(Character.forDigit(0xF & (b >>> 4), 16));
0N/A typeAndValue.append(Character.forDigit(0xF & b, 16));
0N/A }
0N/A } else {
0N/A /*
0N/A * 2.4 (cont): Otherwise, if the AttributeValue is of a type which
0N/A * has a string representation, the value is converted first to a
0N/A * UTF-8 string according to its syntax specification.
0N/A *
0N/A * NOTE: this implementation only emits DirectoryStrings of the
0N/A * types returned by isDerString().
0N/A */
0N/A String valStr = null;
0N/A try {
0N/A valStr = new String(value.getDataBytes(), "UTF8");
0N/A } catch (IOException ie) {
0N/A throw new IllegalArgumentException("DER Value conversion");
0N/A }
0N/A
0N/A /*
0N/A * 2.4 (cont): If the UTF-8 string does not have any of the
0N/A * following characters which need escaping, then that string can be
0N/A * used as the string representation of the value.
0N/A *
0N/A * o a space or "#" character occurring at the beginning of the
0N/A * string
0N/A * o a space character occurring at the end of the string
0N/A * o one of the characters ",", "+", """, "\", "<", ">" or ";"
0N/A *
0N/A * Implementations MAY escape other characters.
0N/A *
0N/A * NOTE: this implementation also recognizes "=" and "#" as
72N/A * characters which need escaping, and null which is escaped as
72N/A * '\00' (see RFC 4514).
0N/A *
0N/A * If a character to be escaped is one of the list shown above, then
0N/A * it is prefixed by a backslash ('\' ASCII 92).
0N/A *
0N/A * Otherwise the character to be escaped is replaced by a backslash
0N/A * and two hex digits, which form a single byte in the code of the
0N/A * character.
0N/A */
0N/A final String escapees = ",=+<>#;\"\\";
0N/A StringBuilder sbuffer = new StringBuilder();
0N/A
0N/A for (int i = 0; i < valStr.length(); i++) {
0N/A char c = valStr.charAt(i);
0N/A if (DerValue.isPrintableStringChar(c) ||
0N/A escapees.indexOf(c) >= 0) {
0N/A
0N/A // escape escapees
0N/A if (escapees.indexOf(c) >= 0) {
0N/A sbuffer.append('\\');
0N/A }
0N/A
0N/A // append printable/escaped char
0N/A sbuffer.append(c);
0N/A
72N/A } else if (c == '\u0000') {
72N/A // escape null character
72N/A sbuffer.append("\\00");
72N/A
0N/A } else if (debug != null && Debug.isOn("ava")) {
0N/A
0N/A // embed non-printable/non-escaped char
0N/A // as escaped hex pairs for debugging
0N/A byte[] valueBytes = null;
0N/A try {
0N/A valueBytes = Character.toString(c).getBytes("UTF8");
0N/A } catch (IOException ie) {
0N/A throw new IllegalArgumentException
0N/A ("DER Value conversion");
0N/A }
0N/A for (int j = 0; j < valueBytes.length; j++) {
0N/A sbuffer.append('\\');
0N/A char hexChar = Character.forDigit
0N/A (0xF & (valueBytes[j] >>> 4), 16);
0N/A sbuffer.append(Character.toUpperCase(hexChar));
0N/A hexChar = Character.forDigit
0N/A (0xF & (valueBytes[j]), 16);
0N/A sbuffer.append(Character.toUpperCase(hexChar));
0N/A }
0N/A } else {
0N/A
0N/A // append non-printable/non-escaped char
0N/A sbuffer.append(c);
0N/A }
0N/A }
0N/A
0N/A char[] chars = sbuffer.toString().toCharArray();
0N/A sbuffer = new StringBuilder();
0N/A
0N/A // Find leading and trailing whitespace.
0N/A int lead; // index of first char that is not leading whitespace
0N/A for (lead = 0; lead < chars.length; lead++) {
0N/A if (chars[lead] != ' ' && chars[lead] != '\r') {
0N/A break;
0N/A }
0N/A }
0N/A int trail; // index of last char that is not trailing whitespace
0N/A for (trail = chars.length - 1; trail >= 0; trail--) {
0N/A if (chars[trail] != ' ' && chars[trail] != '\r') {
0N/A break;
0N/A }
0N/A }
0N/A
0N/A // escape leading and trailing whitespace
0N/A for (int i = 0; i < chars.length; i++) {
0N/A char c = chars[i];
0N/A if (i < lead || i > trail) {
0N/A sbuffer.append('\\');
0N/A }
0N/A sbuffer.append(c);
0N/A }
0N/A typeAndValue.append(sbuffer.toString());
0N/A }
0N/A return typeAndValue.toString();
0N/A }
0N/A
0N/A public String toRFC2253CanonicalString() {
0N/A /*
0N/A * Section 2.3: The AttributeTypeAndValue is encoded as the string
0N/A * representation of the AttributeType, followed by an equals character
0N/A * ('=' ASCII 61), followed by the string representation of the
0N/A * AttributeValue. The encoding of the AttributeValue is given in
0N/A * section 2.4.
0N/A */
0N/A StringBuilder typeAndValue = new StringBuilder(40);
0N/A typeAndValue.append
0N/A (toKeyword(RFC2253, Collections.<String, String>emptyMap()));
0N/A typeAndValue.append('=');
0N/A
0N/A /*
0N/A * Section 2.4: Converting an AttributeValue from ASN.1 to a String.
0N/A * If the AttributeValue is of a type which does not have a string
0N/A * representation defined for it, then it is simply encoded as an
0N/A * octothorpe character ('#' ASCII 35) followed by the hexadecimal
0N/A * representation of each of the bytes of the BER encoding of the X.500
0N/A * AttributeValue. This form SHOULD be used if the AttributeType is of
0N/A * the dotted-decimal form.
0N/A */
0N/A if ((typeAndValue.charAt(0) >= '0' && typeAndValue.charAt(0) <= '9') ||
0N/A !isDerString(value, true))
0N/A {
0N/A byte[] data = null;
0N/A try {
0N/A data = value.toByteArray();
0N/A } catch (IOException ie) {
0N/A throw new IllegalArgumentException("DER Value conversion");
0N/A }
0N/A typeAndValue.append('#');
0N/A for (int j = 0; j < data.length; j++) {
0N/A byte b = data[j];
0N/A typeAndValue.append(Character.forDigit(0xF & (b >>> 4), 16));
0N/A typeAndValue.append(Character.forDigit(0xF & b, 16));
0N/A }
0N/A } else {
0N/A /*
0N/A * 2.4 (cont): Otherwise, if the AttributeValue is of a type which
0N/A * has a string representation, the value is converted first to a
0N/A * UTF-8 string according to its syntax specification.
0N/A *
0N/A * NOTE: this implementation only emits DirectoryStrings of the
0N/A * types returned by isDerString().
0N/A */
0N/A String valStr = null;
0N/A try {
0N/A valStr = new String(value.getDataBytes(), "UTF8");
0N/A } catch (IOException ie) {
0N/A throw new IllegalArgumentException("DER Value conversion");
0N/A }
0N/A
0N/A /*
0N/A * 2.4 (cont): If the UTF-8 string does not have any of the
0N/A * following characters which need escaping, then that string can be
0N/A * used as the string representation of the value.
0N/A *
0N/A * o a space or "#" character occurring at the beginning of the
0N/A * string
0N/A * o a space character occurring at the end of the string
0N/A *
0N/A * o one of the characters ",", "+", """, "\", "<", ">" or ";"
0N/A *
0N/A * If a character to be escaped is one of the list shown above, then
0N/A * it is prefixed by a backslash ('\' ASCII 92).
0N/A *
0N/A * Otherwise the character to be escaped is replaced by a backslash
0N/A * and two hex digits, which form a single byte in the code of the
0N/A * character.
0N/A */
0N/A final String escapees = ",+<>;\"\\";
0N/A StringBuilder sbuffer = new StringBuilder();
0N/A boolean previousWhite = false;
0N/A
0N/A for (int i = 0; i < valStr.length(); i++) {
0N/A char c = valStr.charAt(i);
0N/A
0N/A if (DerValue.isPrintableStringChar(c) ||
0N/A escapees.indexOf(c) >= 0 ||
0N/A (i == 0 && c == '#')) {
0N/A
0N/A // escape leading '#' and escapees
0N/A if ((i == 0 && c == '#') || escapees.indexOf(c) >= 0) {
0N/A sbuffer.append('\\');
0N/A }
0N/A
0N/A // convert multiple whitespace to single whitespace
0N/A if (!Character.isWhitespace(c)) {
0N/A previousWhite = false;
0N/A sbuffer.append(c);
0N/A } else {
0N/A if (previousWhite == false) {
0N/A // add single whitespace
0N/A previousWhite = true;
0N/A sbuffer.append(c);
0N/A } else {
0N/A // ignore subsequent consecutive whitespace
0N/A continue;
0N/A }
0N/A }
0N/A
0N/A } else if (debug != null && Debug.isOn("ava")) {
0N/A
0N/A // embed non-printable/non-escaped char
0N/A // as escaped hex pairs for debugging
0N/A
0N/A previousWhite = false;
0N/A
0N/A byte valueBytes[] = null;
0N/A try {
0N/A valueBytes = Character.toString(c).getBytes("UTF8");
0N/A } catch (IOException ie) {
0N/A throw new IllegalArgumentException
0N/A ("DER Value conversion");
0N/A }
0N/A for (int j = 0; j < valueBytes.length; j++) {
0N/A sbuffer.append('\\');
0N/A sbuffer.append(Character.forDigit
0N/A (0xF & (valueBytes[j] >>> 4), 16));
0N/A sbuffer.append(Character.forDigit
0N/A (0xF & (valueBytes[j]), 16));
0N/A }
0N/A } else {
0N/A
0N/A // append non-printable/non-escaped char
0N/A
0N/A previousWhite = false;
0N/A sbuffer.append(c);
0N/A }
0N/A }
0N/A
0N/A // remove leading and trailing whitespace from value
0N/A typeAndValue.append(sbuffer.toString().trim());
0N/A }
0N/A
0N/A String canon = typeAndValue.toString();
0N/A canon = canon.toUpperCase(Locale.US).toLowerCase(Locale.US);
0N/A return Normalizer.normalize(canon, Normalizer.Form.NFKD);
0N/A }
0N/A
0N/A /*
0N/A * Return true if DerValue can be represented as a String.
0N/A */
0N/A private static boolean isDerString(DerValue value, boolean canonical) {
0N/A if (canonical) {
0N/A switch (value.tag) {
0N/A case DerValue.tag_PrintableString:
0N/A case DerValue.tag_UTF8String:
0N/A return true;
0N/A default:
0N/A return false;
0N/A }
0N/A } else {
0N/A switch (value.tag) {
0N/A case DerValue.tag_PrintableString:
0N/A case DerValue.tag_T61String:
0N/A case DerValue.tag_IA5String:
0N/A case DerValue.tag_GeneralString:
0N/A case DerValue.tag_BMPString:
0N/A case DerValue.tag_UTF8String:
0N/A return true;
0N/A default:
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A boolean hasRFC2253Keyword() {
0N/A return AVAKeyword.hasKeyword(oid, RFC2253);
0N/A }
0N/A
0N/A private String toKeywordValueString(String keyword) {
0N/A /*
0N/A * Construct the value with as little copying and garbage
0N/A * production as practical. First the keyword (mandatory),
0N/A * then the equals sign, finally the value.
0N/A */
0N/A StringBuilder retval = new StringBuilder(40);
0N/A
0N/A retval.append(keyword);
0N/A retval.append("=");
0N/A
0N/A try {
0N/A String valStr = value.getAsString();
0N/A
0N/A if (valStr == null) {
0N/A
0N/A // rfc1779 specifies that attribute values associated
0N/A // with non-standard keyword attributes may be represented
0N/A // using the hex format below. This will be used only
0N/A // when the value is not a string type
0N/A
0N/A byte data [] = value.toByteArray();
0N/A
0N/A retval.append('#');
0N/A for (int i = 0; i < data.length; i++) {
0N/A retval.append(hexDigits.charAt((data [i] >> 4) & 0x0f));
0N/A retval.append(hexDigits.charAt(data [i] & 0x0f));
0N/A }
0N/A
0N/A } else {
0N/A
0N/A boolean quoteNeeded = false;
0N/A StringBuilder sbuffer = new StringBuilder();
0N/A boolean previousWhite = false;
0N/A final String escapees = ",+=\n<>#;\\\"";
0N/A
0N/A /*
0N/A * Special characters (e.g. AVA list separators) cause strings
0N/A * to need quoting, or at least escaping. So do leading or
0N/A * trailing spaces, and multiple internal spaces.
0N/A */
4397N/A int length = valStr.length();
4397N/A boolean alreadyQuoted =
4397N/A (length > 1 && valStr.charAt(0) == '\"'
4397N/A && valStr.charAt(length - 1) == '\"');
4397N/A
4397N/A for (int i = 0; i < length; i++) {
0N/A char c = valStr.charAt(i);
4397N/A if (alreadyQuoted && (i == 0 || i == length - 1)) {
4397N/A sbuffer.append(c);
4397N/A continue;
4397N/A }
0N/A if (DerValue.isPrintableStringChar(c) ||
0N/A escapees.indexOf(c) >= 0) {
0N/A
0N/A // quote if leading whitespace or special chars
0N/A if (!quoteNeeded &&
0N/A ((i == 0 && (c == ' ' || c == '\n')) ||
0N/A escapees.indexOf(c) >= 0)) {
0N/A quoteNeeded = true;
0N/A }
0N/A
0N/A // quote if multiple internal whitespace
0N/A if (!(c == ' ' || c == '\n')) {
0N/A // escape '"' and '\'
0N/A if (c == '"' || c == '\\') {
0N/A sbuffer.append('\\');
0N/A }
0N/A previousWhite = false;
0N/A } else {
0N/A if (!quoteNeeded && previousWhite) {
0N/A quoteNeeded = true;
0N/A }
0N/A previousWhite = true;
0N/A }
0N/A
0N/A sbuffer.append(c);
0N/A
0N/A } else if (debug != null && Debug.isOn("ava")) {
0N/A
0N/A // embed non-printable/non-escaped char
0N/A // as escaped hex pairs for debugging
0N/A
0N/A previousWhite = false;
0N/A
0N/A // embed escaped hex pairs
0N/A byte[] valueBytes =
0N/A Character.toString(c).getBytes("UTF8");
0N/A for (int j = 0; j < valueBytes.length; j++) {
0N/A sbuffer.append('\\');
0N/A char hexChar = Character.forDigit
0N/A (0xF & (valueBytes[j] >>> 4), 16);
0N/A sbuffer.append(Character.toUpperCase(hexChar));
0N/A hexChar = Character.forDigit
0N/A (0xF & (valueBytes[j]), 16);
0N/A sbuffer.append(Character.toUpperCase(hexChar));
0N/A }
0N/A } else {
0N/A
0N/A // append non-printable/non-escaped char
0N/A
0N/A previousWhite = false;
0N/A sbuffer.append(c);
0N/A }
0N/A }
0N/A
0N/A // quote if trailing whitespace
0N/A if (sbuffer.length() > 0) {
0N/A char trailChar = sbuffer.charAt(sbuffer.length() - 1);
0N/A if (trailChar == ' ' || trailChar == '\n') {
0N/A quoteNeeded = true;
0N/A }
0N/A }
0N/A
0N/A // Emit the string ... quote it if needed
4397N/A // if string is already quoted, don't re-quote
4397N/A if (!alreadyQuoted && quoteNeeded) {
0N/A retval.append("\"" + sbuffer.toString() + "\"");
0N/A } else {
0N/A retval.append(sbuffer.toString());
0N/A }
0N/A }
0N/A } catch (IOException e) {
0N/A throw new IllegalArgumentException("DER Value conversion");
0N/A }
0N/A
0N/A return retval.toString();
0N/A }
0N/A
0N/A}
0N/A
0N/A/**
0N/A * Helper class that allows conversion from String to ObjectIdentifier and
0N/A * vice versa according to RFC1779, RFC2253, and an augmented version of
0N/A * those standards.
0N/A */
0N/Aclass AVAKeyword {
0N/A
0N/A private static final Map<ObjectIdentifier,AVAKeyword> oidMap;
0N/A private static final Map<String,AVAKeyword> keywordMap;
0N/A
0N/A private String keyword;
0N/A private ObjectIdentifier oid;
0N/A private boolean rfc1779Compliant, rfc2253Compliant;
0N/A
0N/A private AVAKeyword(String keyword, ObjectIdentifier oid,
0N/A boolean rfc1779Compliant, boolean rfc2253Compliant) {
0N/A this.keyword = keyword;
0N/A this.oid = oid;
0N/A this.rfc1779Compliant = rfc1779Compliant;
0N/A this.rfc2253Compliant = rfc2253Compliant;
0N/A
0N/A // register it
0N/A oidMap.put(oid, this);
0N/A keywordMap.put(keyword, this);
0N/A }
0N/A
0N/A private boolean isCompliant(int standard) {
0N/A switch (standard) {
0N/A case AVA.RFC1779:
0N/A return rfc1779Compliant;
0N/A case AVA.RFC2253:
0N/A return rfc2253Compliant;
0N/A case AVA.DEFAULT:
0N/A return true;
0N/A default:
0N/A // should not occur, internal error
0N/A throw new IllegalArgumentException("Invalid standard " + standard);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get an object identifier representing the specified keyword (or
0N/A * string encoded object identifier) in the given standard.
0N/A *
0N/A * @throws IOException If the keyword is not valid in the specified standard
0N/A */
0N/A static ObjectIdentifier getOID(String keyword, int standard)
0N/A throws IOException {
0N/A return getOID
0N/A (keyword, standard, Collections.<String, String>emptyMap());
0N/A }
0N/A
0N/A /**
0N/A * Get an object identifier representing the specified keyword (or
0N/A * string encoded object identifier) in the given standard.
0N/A *
0N/A * @param keywordMap a Map where a keyword String maps to a corresponding
0N/A * OID String. Each AVA keyword will be mapped to the corresponding OID.
0N/A * If an entry does not exist, it will fallback to the builtin
0N/A * keyword/OID mapping.
0N/A * @throws IOException If the keyword is not valid in the specified standard
0N/A * or the OID String to which a keyword maps to is improperly formatted.
0N/A */
0N/A static ObjectIdentifier getOID
0N/A (String keyword, int standard, Map<String, String> extraKeywordMap)
0N/A throws IOException {
0N/A
2619N/A keyword = keyword.toUpperCase(Locale.ENGLISH);
0N/A if (standard == AVA.RFC2253) {
0N/A if (keyword.startsWith(" ") || keyword.endsWith(" ")) {
0N/A throw new IOException("Invalid leading or trailing space " +
0N/A "in keyword \"" + keyword + "\"");
0N/A }
0N/A } else {
0N/A keyword = keyword.trim();
0N/A }
0N/A
0N/A // check user-specified keyword map first, then fallback to built-in
0N/A // map
0N/A String oidString = extraKeywordMap.get(keyword);
0N/A if (oidString == null) {
0N/A AVAKeyword ak = keywordMap.get(keyword);
0N/A if ((ak != null) && ak.isCompliant(standard)) {
0N/A return ak.oid;
0N/A }
0N/A } else {
0N/A return new ObjectIdentifier(oidString);
0N/A }
0N/A
0N/A // no keyword found or not standard compliant, check if OID string
0N/A
0N/A // RFC1779 requires, DEFAULT allows OID. prefix
0N/A if (standard == AVA.RFC1779) {
0N/A if (keyword.startsWith("OID.") == false) {
0N/A throw new IOException("Invalid RFC1779 keyword: " + keyword);
0N/A }
0N/A keyword = keyword.substring(4);
0N/A } else if (standard == AVA.DEFAULT) {
0N/A if (keyword.startsWith("OID.")) {
0N/A keyword = keyword.substring(4);
0N/A }
0N/A }
0N/A boolean number = false;
0N/A if (keyword.length() != 0) {
0N/A char ch = keyword.charAt(0);
0N/A if ((ch >= '0') && (ch <= '9')) {
0N/A number = true;
0N/A }
0N/A }
0N/A if (number == false) {
0N/A throw new IOException("Invalid keyword \"" + keyword + "\"");
0N/A }
0N/A return new ObjectIdentifier(keyword);
0N/A }
0N/A
0N/A /**
0N/A * Get a keyword for the given ObjectIdentifier according to standard.
0N/A * If no keyword is available, the ObjectIdentifier is encoded as a
0N/A * String.
0N/A */
0N/A static String getKeyword(ObjectIdentifier oid, int standard) {
0N/A return getKeyword
0N/A (oid, standard, Collections.<String, String>emptyMap());
0N/A }
0N/A
0N/A /**
0N/A * Get a keyword for the given ObjectIdentifier according to standard.
0N/A * Checks the extraOidMap for a keyword first, then falls back to the
0N/A * builtin/default set. If no keyword is available, the ObjectIdentifier
0N/A * is encoded as a String.
0N/A */
0N/A static String getKeyword
0N/A (ObjectIdentifier oid, int standard, Map<String, String> extraOidMap) {
0N/A
0N/A // check extraOidMap first, then fallback to built-in map
0N/A String oidString = oid.toString();
0N/A String keywordString = extraOidMap.get(oidString);
0N/A if (keywordString == null) {
0N/A AVAKeyword ak = oidMap.get(oid);
0N/A if ((ak != null) && ak.isCompliant(standard)) {
0N/A return ak.keyword;
0N/A }
0N/A } else {
0N/A if (keywordString.length() == 0) {
0N/A throw new IllegalArgumentException("keyword cannot be empty");
0N/A }
0N/A keywordString = keywordString.trim();
0N/A char c = keywordString.charAt(0);
0N/A if (c < 65 || c > 122 || (c > 90 && c < 97)) {
0N/A throw new IllegalArgumentException
0N/A ("keyword does not start with letter");
0N/A }
0N/A for (int i=1; i<keywordString.length(); i++) {
0N/A c = keywordString.charAt(i);
0N/A if ((c < 65 || c > 122 || (c > 90 && c < 97)) &&
0N/A (c < 48 || c > 57) && c != '_') {
0N/A throw new IllegalArgumentException
0N/A ("keyword character is not a letter, digit, or underscore");
0N/A }
0N/A }
0N/A return keywordString;
0N/A }
0N/A // no compliant keyword, use OID
0N/A if (standard == AVA.RFC2253) {
0N/A return oidString;
0N/A } else {
0N/A return "OID." + oidString;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Test if oid has an associated keyword in standard.
0N/A */
0N/A static boolean hasKeyword(ObjectIdentifier oid, int standard) {
0N/A AVAKeyword ak = oidMap.get(oid);
0N/A if (ak == null) {
0N/A return false;
0N/A }
0N/A return ak.isCompliant(standard);
0N/A }
0N/A
0N/A static {
0N/A oidMap = new HashMap<ObjectIdentifier,AVAKeyword>();
0N/A keywordMap = new HashMap<String,AVAKeyword>();
0N/A
0N/A // NOTE if multiple keywords are available for one OID, order
0N/A // is significant!! Preferred *LAST*.
0N/A new AVAKeyword("CN", X500Name.commonName_oid, true, true);
0N/A new AVAKeyword("C", X500Name.countryName_oid, true, true);
0N/A new AVAKeyword("L", X500Name.localityName_oid, true, true);
0N/A new AVAKeyword("S", X500Name.stateName_oid, false, false);
0N/A new AVAKeyword("ST", X500Name.stateName_oid, true, true);
0N/A new AVAKeyword("O", X500Name.orgName_oid, true, true);
0N/A new AVAKeyword("OU", X500Name.orgUnitName_oid, true, true);
0N/A new AVAKeyword("T", X500Name.title_oid, false, false);
0N/A new AVAKeyword("IP", X500Name.ipAddress_oid, false, false);
0N/A new AVAKeyword("STREET", X500Name.streetAddress_oid,true, true);
0N/A new AVAKeyword("DC", X500Name.DOMAIN_COMPONENT_OID,
0N/A false, true);
0N/A new AVAKeyword("DNQUALIFIER", X500Name.DNQUALIFIER_OID, false, false);
0N/A new AVAKeyword("DNQ", X500Name.DNQUALIFIER_OID, false, false);
0N/A new AVAKeyword("SURNAME", X500Name.SURNAME_OID, false, false);
0N/A new AVAKeyword("GIVENNAME", X500Name.GIVENNAME_OID, false, false);
0N/A new AVAKeyword("INITIALS", X500Name.INITIALS_OID, false, false);
0N/A new AVAKeyword("GENERATION", X500Name.GENERATIONQUALIFIER_OID,
0N/A false, false);
0N/A new AVAKeyword("EMAIL", PKCS9Attribute.EMAIL_ADDRESS_OID, false, false);
0N/A new AVAKeyword("EMAILADDRESS", PKCS9Attribute.EMAIL_ADDRESS_OID,
0N/A false, false);
0N/A new AVAKeyword("UID", X500Name.userid_oid, false, true);
0N/A new AVAKeyword("SERIALNUMBER", X500Name.SERIALNUMBER_OID, false, false);
0N/A }
0N/A}