⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 x509name.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }

    /**
     * return false if we have characters out of the range of a printable
     * string, true otherwise.
     */
    private boolean canBePrintable(
        String  str)
    {
        for (int i = str.length() - 1; i >= 0; i--)
        {
            if (str.charAt(i) > 0x007f)
            {
                return false;
            }
        }

        return true;
    }

    public DERObject getDERObject()
    {
        if (seq == null)
        {
            ASN1EncodableVector  vec = new ASN1EncodableVector();

            for (int i = 0; i != ordering.size(); i++)
            {
                ASN1EncodableVector     v = new ASN1EncodableVector();
                DERObjectIdentifier     oid = (DERObjectIdentifier)ordering.elementAt(i);

                v.add(oid);

                String  str = (String)values.elementAt(i);

				if (str.charAt(0) == '#')
				{
					str = str.toLowerCase();
					byte[]	data = new byte[str.length() / 2];
					for (int index = 0; index != data.length; index++)
					{
						char	left = str.charAt((index * 2) + 1);
						char right = str.charAt((index * 2) + 2);

						if (left < 'a')
						{
							data[index] = (byte)((left - '0') << 4);
						}
						else
						{
							data[index] = (byte)((left - 'a' + 10) << 4);
						}
						if (right < 'a')
						{
							data[index] |= (byte)(right - '0');
						}
						else
						{
							data[index] |= (byte)(right - 'a' + 10);
						}
					}

					ASN1InputStream aIn = new ASN1InputStream(
														new ByteArrayInputStream(data));

					try
                    {
                        v.add(aIn.readObject());
                    }
                    catch (IOException e)
                    {
                        throw new RuntimeException("bad object in '#' string");
                    }
				}
				else
				{
	                if (oid.equals(EmailAddress))
	                {
	                    v.add(new DERIA5String(str));
	                }
	                else
	                {
	                    if (canBePrintable(str))
	                    {
	                        v.add(new DERPrintableString(str));
	                    }
	                    else
	                    {
	                        v.add(new DERUTF8String(str));
	                    }
	                }
				}

                vec.add(new DERSet(new DERSequence(v)));
            }

            seq = new DERSequence(vec);
        }

        return seq;
    }

    /**
     * @param inOrder if true the order of both X509 names must be the same,
     * as well as the values associated with each element.
     */
    public boolean equals(Object _obj, boolean inOrder)
    {
        if (_obj == this)
        {
            return true;
        }

        if (!inOrder)
        {
            return this.equals(_obj);
        }

        if (_obj == null || !(_obj instanceof X509Name))
        {
            return false;
        }

        X509Name _oxn          = (X509Name)_obj;
        int      _orderingSize = ordering.size();

        if (_orderingSize != _oxn.ordering.size())
        {
			return false;
		}

		for(int i = 0; i < _orderingSize; i++)
		{
			String  _oid   = ((DERObjectIdentifier)ordering.elementAt(i)).getId();
			String  _val   = (String)values.elementAt(i);

            String _oOID = ((DERObjectIdentifier)_oxn.ordering.elementAt(i)).getId();
            String _oVal = (String)_oxn.values.elementAt(i);

            if (_oid.equals(_oOID))
            {
                _val = _val.trim().toLowerCase();
                _oVal = _oVal.trim().toLowerCase();
                if (_val.equals(_oVal))
                {
                    continue;
                }
                else
                {
                    StringBuffer    v1 = new StringBuffer();
                    StringBuffer    v2 = new StringBuffer();

                    if (_val.length() != 0)
                    {
                        char    c1 = _val.charAt(0);

                        v1.append(c1);

                        for (int k = 1; k < _val.length(); k++)
                        {
                            char    c2 = _val.charAt(k);
                            if (!(c1 == ' ' && c2 == ' '))
                            {
                                v1.append(c2);
                            }
                            c1 = c2;
                        }
                    }

                    if (_oVal.length() != 0)
                    {
                        char    c1 = _oVal.charAt(0);

                        v2.append(c1);

                        for (int k = 1; k < _oVal.length(); k++)
                        {
                            char    c2 = _oVal.charAt(k);
                            if (!(c1 == ' ' && c2 == ' '))
                            {
                                v2.append(c2);
                            }
                            c1 = c2;
                        }
                    }

                    if (!v1.toString().equals(v2.toString()))
                    {
                        return false;
                    }
                }
            }
        }

		return true;
	}

    /**
     * test for equality - note: case is ignored.
     */
    public boolean equals(Object _obj)
    {
        if (_obj == this)
        {
            return true;
        }

        if (_obj == null || !(_obj instanceof X509Name))
        {
            return false;
        }

        X509Name _oxn          = (X509Name)_obj;

        if (this.getDERObject().equals(_oxn.getDERObject()))
        {
        	return true;
        }

        int      _orderingSize = ordering.size();

        if (_orderingSize != _oxn.ordering.size())
        {
			return false;
		}

		boolean[] _indexes = new boolean[_orderingSize];

		for(int i = 0; i < _orderingSize; i++)
		{
			boolean _found = false;
			String  _oid   = ((DERObjectIdentifier)ordering.elementAt(i)).getId();
			String  _val   = (String)values.elementAt(i);

			for(int j = 0; j < _orderingSize; j++)
			{
				if(_indexes[j] == true)
				{
					continue;
				}

				String _oOID = ((DERObjectIdentifier)_oxn.ordering.elementAt(j)).getId();
				String _oVal = (String)_oxn.values.elementAt(j);

				if (_oid.equals(_oOID))
                {
                    _val = _val.trim().toLowerCase();
                    _oVal = _oVal.trim().toLowerCase();
                    if (_val.equals(_oVal))
                    {
                        _indexes[j] = true;
                        _found      = true;
                        break;
                    }
                    else
                    {
                        StringBuffer    v1 = new StringBuffer();
                        StringBuffer    v2 = new StringBuffer();

                        if (_val.length() != 0)
                        {
                            char    c1 = _val.charAt(0);

                            v1.append(c1);

                            for (int k = 1; k < _val.length(); k++)
                            {
                                char    c2 = _val.charAt(k);
                                if (!(c1 == ' ' && c2 == ' '))
                                {
                                    v1.append(c2);
                                }
                                c1 = c2;
                            }
                        }

                        if (_oVal.length() != 0)
                        {
                            char    c1 = _oVal.charAt(0);

                            v2.append(c1);

                            for (int k = 1; k < _oVal.length(); k++)
                            {
                                char    c2 = _oVal.charAt(k);
                                if (!(c1 == ' ' && c2 == ' '))
                                {
                                    v2.append(c2);
                                }
                                c1 = c2;
                            }
                        }

                        if (v1.toString().equals(v2.toString()))
                        {
                            _indexes[j] = true;
                            _found      = true;
                            break;
                        }
                    }
                }
			}

			if(!_found)
			{
				return false;
			}
		}

		return true;
	}

    public int hashCode()
    {
        ASN1Sequence  seq = (ASN1Sequence)this.getDERObject();
        Enumeration   e = seq.getObjects();
        int           hashCode = 0;

        while (e.hasMoreElements())
        {
            hashCode ^= e.nextElement().hashCode();
        }

        return hashCode;
    }

    private void appendValue(
        StringBuffer        buf,
        Hashtable           oidSymbols,
        DERObjectIdentifier oid,
        String              value)
    {
        String  sym = (String)oidSymbols.get(oid);

        if (sym != null)
        {
            buf.append(sym);
        }
        else
        {
            buf.append(oid.getId());
        }

        buf.append("=");

        int     index = buf.length();

        buf.append(value);

        int     end = buf.length();

        while (index != end)
        {
            if ((buf.charAt(index) == ',')
               || (buf.charAt(index) == '"')
               || (buf.charAt(index) == '\\')
               || (buf.charAt(index) == '+')
               || (buf.charAt(index) == '<')
               || (buf.charAt(index) == '>')
               || (buf.charAt(index) == ';'))
            {
                buf.insert(index, "\\");
                index++;
                end++;
            }

            index++;
        }
    }

    /**
     * convert the structure to a string - if reverse is true the
     * oids and values are listed out starting with the last element
     * in the sequence (ala RFC 2253), otherwise the string will begin
     * with the first element of the structure. If no string definition
     * for the oid is found in oidSymbols the string value of the oid is
     * added. Two standard symbol tables are provided DefaultSymbols, and
     * RFC2253Symbols as part of this class.
     *
     * @param reverse if true start at the end of the sequence and work back.
     * @param oidSymbols look up table strings for oids.
     */
    public String toString(
        boolean     reverse,
        Hashtable   oidSymbols)
    {
        StringBuffer            buf = new StringBuffer();
        boolean                 first = true;

        if (reverse)
        {
            for (int i = ordering.size() - 1; i >= 0; i--)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
					if (((Boolean)added.elementAt(i + 1)).booleanValue())
					{
						buf.append(" + ");
					}
					else
					{
						buf.append(",");
					}
                }

                appendValue(buf, oidSymbols,
                            (DERObjectIdentifier)ordering.elementAt(i),
                            (String)values.elementAt(i));
            }
        }
        else
        {
            for (int i = 0; i < ordering.size(); i++)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                	if (((Boolean)added.elementAt(i)).booleanValue())
                	{
                		buf.append(" + ");
                	}
                	else
                	{
						buf.append(",");
                	}
                }

                appendValue(buf, oidSymbols,
                            (DERObjectIdentifier)ordering.elementAt(i),
                            (String)values.elementAt(i));
            }
        }

        return buf.toString();
    }

    public String toString()
    {
        return toString(DefaultReverse, DefaultSymbols);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -