Ejemplo n.º 1
0
        public virtual bool equals(RDN rdn)
        {
            if (values.Count != rdn.values.Count)
            {
                return(false);
            }
            int j, i;

            for (i = 0; i < values.Count; i++)
            {
                //verify that the current value and type exists in the other list
                j = 0;
                //May need a more intellegent compare
                while (j < values.Count && (!((string)values[i]).ToUpper().Equals(((string)rdn.values[j]).ToUpper()) || !equalAttrType((string)types[i], (string)rdn.types[j])))
                {
                    j++;
                }
                if (j >= rdn.values.Count)
                {
                    //couldn't find first value
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 2
0
        private string rawValue;  //the unnormalized value

        /// <summary> Creates an RDN object from the DN component specified in the string RDN
        ///
        /// </summary>
        /// <param name="rdn">the DN component
        /// </param>
        public RDN(string rdn)
        {
            rawValue = rdn;
            DN        dn   = new DN(rdn);
            ArrayList rdns = dn.RDNs;

            //there should only be one rdn
            if (rdns.Count != 1)
            {
                throw new ArgumentException("Invalid RDN: see API " + "documentation");
            }
            RDN thisRDN = (RDN)(rdns[0]);

            types    = thisRDN.types;
            values   = thisRDN.values;
            rawValue = thisRDN.rawValue;
            return;
        }
Ejemplo n.º 3
0
 /// <summary> Adds the RDN to the beginning of the current DN.</summary>
 /// <param name="rdn">an RDN to be added
 /// </param>
 public virtual void addRDNToFront(RDN rdn)
 {
     rdnList.Insert(0, rdn);
 }
Ejemplo n.º 4
0
 /// <summary> Adds the RDN to the end of the current DN</summary>
 /// <param name="rdn">an RDN to be added
 /// </param>
 public virtual void addRDNToBack(RDN rdn)
 {
     rdnList.Add(rdn);
 }
Ejemplo n.º 5
0
        /// <summary> Constructs a new DN based on the specified string representation of a
        /// distinguished name. The syntax of the DN must conform to that specified
        /// in RFC 2253.
        ///
        /// </summary>
        /// <param name="dnString">a string representation of the distinguished name
        /// </param>
        /// <exception>  IllegalArgumentException  if the the value of the dnString
        /// parameter does not adhere to the syntax described in
        /// RFC 2253
        /// </exception>
        public DN(string dnString)
        {
            InitBlock();
            /* the empty string is a valid DN */
            if (dnString.Length == 0)
            {
                return;
            }

            char currChar;
            char nextChar;
            int  currIndex;

            char[] tokenBuf = new char[dnString.Length];
            int    tokenIndex;
            int    lastIndex;
            int    valueStart;
            int    state;
            int    trailingSpaceCount = 0;
            string attrType           = "";
            string attrValue          = "";
            string rawValue           = "";
            int    hexDigitCount      = 0;
            RDN    currRDN            = new RDN();

            //indicates whether an OID number has a first digit of ZERO
            bool firstDigitZero = false;

            tokenIndex = 0;
            currIndex  = 0;
            valueStart = 0;
            state      = LOOK_FOR_RDN_ATTR_TYPE;
            lastIndex  = dnString.Length - 1;
            while (currIndex <= lastIndex)
            {
                currChar = dnString[currIndex];
                switch (state)
                {
                case LOOK_FOR_RDN_ATTR_TYPE:
                    while (currChar == ' ' && (currIndex < lastIndex))
                    {
                        currChar = dnString[++currIndex];
                    }
                    if (isAlpha(currChar))
                    {
                        if (dnString.Substring(currIndex).StartsWith("oid.") || dnString.Substring(currIndex).StartsWith("OID."))
                        {
                            //form is "oid.###.##.###... or OID.###.##.###...
                            currIndex += 4;     //skip oid. prefix and get to actual oid
                            if (currIndex > lastIndex)
                            {
                                throw new ArgumentException(dnString);
                            }
                            currChar = dnString[currIndex];
                            if (isDigit(currChar))
                            {
                                tokenBuf[tokenIndex++] = currChar;
                                state = OID_ATTR_TYPE;
                            }
                            else
                            {
                                throw new ArgumentException(dnString);
                            }
                        }
                        else
                        {
                            tokenBuf[tokenIndex++] = currChar;
                            state = ALPHA_ATTR_TYPE;
                        }
                    }
                    else if (isDigit(currChar))
                    {
                        --currIndex;
                        state = OID_ATTR_TYPE;
                    }
                    else if (System.Globalization.CharUnicodeInfo.GetUnicodeCategory(currChar) != System.Globalization.UnicodeCategory.SpaceSeparator)
                    {
                        throw new ArgumentException(dnString);
                    }
                    break;


                case ALPHA_ATTR_TYPE:
                    if (isAlpha(currChar) || isDigit(currChar) || (currChar == '-'))
                    {
                        tokenBuf[tokenIndex++] = currChar;
                    }
                    else
                    {
                        //skip any spaces
                        while ((currChar == ' ') && (currIndex < lastIndex))
                        {
                            currChar = dnString[++currIndex];
                        }
                        if (currChar == '=')
                        {
                            attrType   = new string(tokenBuf, 0, tokenIndex);
                            tokenIndex = 0;
                            state      = LOOK_FOR_RDN_VALUE;
                        }
                        else
                        {
                            throw new ArgumentException(dnString);
                        }
                    }
                    break;


                case OID_ATTR_TYPE:
                    if (!isDigit(currChar))
                    {
                        throw new ArgumentException(dnString);
                    }
                    firstDigitZero         = (currChar == '0');
                    tokenBuf[tokenIndex++] = currChar;
                    currChar = dnString[++currIndex];

                    if ((isDigit(currChar) && firstDigitZero) || (currChar == '.' && firstDigitZero))
                    {
                        throw new ArgumentException(dnString);
                    }

                    //consume all numbers.
                    while (isDigit(currChar) && (currIndex < lastIndex))
                    {
                        tokenBuf[tokenIndex++] = currChar;
                        currChar = dnString[++currIndex];
                    }
                    if (currChar == '.')
                    {
                        tokenBuf[tokenIndex++] = currChar;
                        //The state remains at OID_ATTR_TYPE
                    }
                    else
                    {
                        //skip any spaces
                        while (currChar == ' ' && (currIndex < lastIndex))
                        {
                            currChar = dnString[++currIndex];
                        }
                        if (currChar == '=')
                        {
                            attrType   = new string(tokenBuf, 0, tokenIndex);
                            tokenIndex = 0;
                            state      = LOOK_FOR_RDN_VALUE;
                        }
                        else
                        {
                            throw new ArgumentException(dnString);
                        }
                    }
                    break;


                case LOOK_FOR_RDN_VALUE:
                    while (currChar == ' ')
                    {
                        if (currIndex < lastIndex)
                        {
                            currChar = dnString[++currIndex];
                        }
                        else
                        {
                            throw new ArgumentException(dnString);
                        }
                    }
                    if (currChar == '"')
                    {
                        state      = QUOTED_RDN_VALUE;
                        valueStart = currIndex;
                    }
                    else if (currChar == '#')
                    {
                        hexDigitCount          = 0;
                        tokenBuf[tokenIndex++] = currChar;
                        valueStart             = currIndex;
                        state = HEX_RDN_VALUE;
                    }
                    else
                    {
                        valueStart = currIndex;
                        //check this character again in the UNQUOTED_RDN_VALUE state
                        currIndex--;
                        state = UNQUOTED_RDN_VALUE;
                    }
                    break;


                case UNQUOTED_RDN_VALUE:
                    if (currChar == '\\')
                    {
                        if (!(currIndex < lastIndex))
                        {
                            throw new ArgumentException(dnString);
                        }
                        currChar = dnString[++currIndex];
                        if (isHexDigit(currChar))
                        {
                            if (!(currIndex < lastIndex))
                            {
                                throw new ArgumentException(dnString);
                            }
                            nextChar = dnString[++currIndex];
                            if (isHexDigit(nextChar))
                            {
                                tokenBuf[tokenIndex++] = hexToChar(currChar, nextChar);
                                trailingSpaceCount     = 0;
                            }
                            else
                            {
                                throw new ArgumentException(dnString);
                            }
                        }
                        else if (needsEscape(currChar) || currChar == '#' || currChar == '=' || currChar == ' ')
                        {
                            tokenBuf[tokenIndex++] = currChar;
                            trailingSpaceCount     = 0;
                        }
                        else
                        {
                            throw new ArgumentException(dnString);
                        }
                    }
                    else if (currChar == ' ')
                    {
                        trailingSpaceCount++;
                        tokenBuf[tokenIndex++] = currChar;
                    }
                    else if ((currChar == ',') || (currChar == ';') || (currChar == '+'))
                    {
                        attrValue = new string(tokenBuf, 0, tokenIndex - trailingSpaceCount);
                        rawValue  = dnString.Substring(valueStart, (currIndex - trailingSpaceCount) - (valueStart));

                        currRDN.add(attrType, attrValue, rawValue);
                        if (currChar != '+')
                        {
                            rdnList.Add(currRDN);
                            currRDN = new RDN();
                        }

                        trailingSpaceCount = 0;
                        tokenIndex         = 0;
                        state = LOOK_FOR_RDN_ATTR_TYPE;
                    }
                    else if (needsEscape(currChar))
                    {
                        throw new ArgumentException(dnString);
                    }
                    else
                    {
                        trailingSpaceCount     = 0;
                        tokenBuf[tokenIndex++] = currChar;
                    }
                    break;     //end UNQUOTED RDN VALUE


                case QUOTED_RDN_VALUE:
                    if (currChar == '"')
                    {
                        rawValue = dnString.Substring(valueStart, (currIndex + 1) - (valueStart));
                        if (currIndex < lastIndex)
                        {
                            currChar = dnString[++currIndex];
                        }
                        //skip any spaces
                        while ((currChar == ' ') && (currIndex < lastIndex))
                        {
                            currChar = dnString[++currIndex];
                        }
                        if ((currChar == ',') || (currChar == ';') || (currChar == '+') || (currIndex == lastIndex))
                        {
                            attrValue = new string(tokenBuf, 0, tokenIndex);

                            currRDN.add(attrType, attrValue, rawValue);
                            if (currChar != '+')
                            {
                                rdnList.Add(currRDN);
                                currRDN = new RDN();
                            }
                            trailingSpaceCount = 0;
                            tokenIndex         = 0;
                            state = LOOK_FOR_RDN_ATTR_TYPE;
                        }
                        else
                        {
                            throw new ArgumentException(dnString);
                        }
                    }
                    else if (currChar == '\\')
                    {
                        currChar = dnString[++currIndex];
                        if (isHexDigit(currChar))
                        {
                            nextChar = dnString[++currIndex];
                            if (isHexDigit(nextChar))
                            {
                                tokenBuf[tokenIndex++] = hexToChar(currChar, nextChar);
                                trailingSpaceCount     = 0;
                            }
                            else
                            {
                                throw new ArgumentException(dnString);
                            }
                        }
                        else if (needsEscape(currChar) || currChar == '#' || currChar == '=' || currChar == ' ')
                        {
                            tokenBuf[tokenIndex++] = currChar;
                            trailingSpaceCount     = 0;
                        }
                        else
                        {
                            throw new ArgumentException(dnString);
                        }
                    }
                    else
                    {
                        tokenBuf[tokenIndex++] = currChar;
                    }
                    break;     //end QUOTED RDN VALUE


                case HEX_RDN_VALUE:
                    if ((!isHexDigit(currChar)) || (currIndex > lastIndex))
                    {
                        //check for odd number of hex digits
                        if ((hexDigitCount % 2) != 0 || hexDigitCount == 0)
                        {
                            throw new ArgumentException(dnString);
                        }
                        else
                        {
                            rawValue = dnString.Substring(valueStart, (currIndex) - (valueStart));
                            //skip any spaces
                            while ((currChar == ' ') && (currIndex < lastIndex))
                            {
                                currChar = dnString[++currIndex];
                            }
                            if ((currChar == ',') || (currChar == ';') || (currChar == '+') || (currIndex == lastIndex))
                            {
                                attrValue = new string(tokenBuf, 0, tokenIndex);

                                //added by cameron
                                currRDN.add(attrType, attrValue, rawValue);
                                if (currChar != '+')
                                {
                                    rdnList.Add(currRDN);
                                    currRDN = new RDN();
                                }
                                tokenIndex = 0;
                                state      = LOOK_FOR_RDN_ATTR_TYPE;
                            }
                            else
                            {
                                throw new ArgumentException(dnString);
                            }
                        }
                    }
                    else
                    {
                        tokenBuf[tokenIndex++] = currChar;
                        hexDigitCount++;
                    }
                    break;     //end HEX RDN VALUE
                } //end switch
                currIndex++;
            } //end while

            //check ending state
            if (state == UNQUOTED_RDN_VALUE || (state == HEX_RDN_VALUE && (hexDigitCount % 2) == 0) && hexDigitCount != 0)
            {
                attrValue = new string(tokenBuf, 0, tokenIndex - trailingSpaceCount);
                rawValue  = dnString.Substring(valueStart, (currIndex - trailingSpaceCount) - (valueStart));
                currRDN.add(attrType, attrValue, rawValue);
                rdnList.Add(currRDN);
            }
            else if (state == LOOK_FOR_RDN_VALUE)
            {
                //empty value is valid
                attrValue = "";
                rawValue  = dnString.Substring(valueStart);
                currRDN.add(attrType, attrValue, rawValue);
                rdnList.Add(currRDN);
            }
            else
            {
                throw new ArgumentException(dnString);
            }
        } //end DN constructor (string dn)