001package org.apache.commons.ssl.org.bouncycastle.asn1.x509;
002
003import java.io.IOException;
004
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1ObjectIdentifier;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
007import org.apache.commons.ssl.org.bouncycastle.asn1.DERGeneralizedTime;
008import org.apache.commons.ssl.org.bouncycastle.asn1.DERIA5String;
009import org.apache.commons.ssl.org.bouncycastle.asn1.DERPrintableString;
010import org.apache.commons.ssl.org.bouncycastle.asn1.DERUTF8String;
011
012/**
013 * The default converter for X509 DN entries when going from their
014 * string value to ASN.1 strings.
015 */
016public class X509DefaultEntryConverter
017    extends X509NameEntryConverter
018{
019    /**
020     * Apply default coversion for the given value depending on the oid
021     * and the character range of the value.
022     * 
023     * @param oid the object identifier for the DN entry
024     * @param value the value associated with it
025     * @return the ASN.1 equivalent for the string value.
026     */
027    public ASN1Primitive getConvertedValue(
028        ASN1ObjectIdentifier  oid,
029        String               value)
030    {
031        if (value.length() != 0 && value.charAt(0) == '#')
032        {
033            try
034            {
035                return convertHexEncoded(value, 1);
036            }
037            catch (IOException e)
038            {
039                throw new RuntimeException("can't recode value for oid " + oid.getId());
040            }
041        }
042        else
043        {
044            if (value.length() != 0 && value.charAt(0) == '\\')
045            {
046                value = value.substring(1);
047            }
048            if (oid.equals(X509Name.EmailAddress) || oid.equals(X509Name.DC))
049            {
050                return new DERIA5String(value);
051            }
052            else if (oid.equals(X509Name.DATE_OF_BIRTH))  // accept time string as well as # (for compatibility)
053            {
054                return new DERGeneralizedTime(value);
055            }
056            else if (oid.equals(X509Name.C) || oid.equals(X509Name.SN) || oid.equals(X509Name.DN_QUALIFIER)
057                || oid.equals(X509Name.TELEPHONE_NUMBER))
058            {
059                 return new DERPrintableString(value);
060            }
061        }
062        
063        return new DERUTF8String(value);
064    }
065}