1package org.bouncycastle.asn1.x509;
2
3import java.io.IOException;
4
5import org.bouncycastle.asn1.ASN1ObjectIdentifier;
6import org.bouncycastle.asn1.ASN1Primitive;
7import org.bouncycastle.asn1.DERGeneralizedTime;
8import org.bouncycastle.asn1.DERIA5String;
9import org.bouncycastle.asn1.DERPrintableString;
10import org.bouncycastle.asn1.DERUTF8String;
11
12/**
13 * The default converter for X509 DN entries when going from their
14 * string value to ASN.1 strings.
15 */
16public class X509DefaultEntryConverter
17    extends X509NameEntryConverter
18{
19    /**
20     * Apply default coversion for the given value depending on the oid
21     * and the character range of the value.
22     *
23     * @param oid the object identifier for the DN entry
24     * @param value the value associated with it
25     * @return the ASN.1 equivalent for the string value.
26     */
27    public ASN1Primitive getConvertedValue(
28        ASN1ObjectIdentifier  oid,
29        String               value)
30    {
31        if (value.length() != 0 && value.charAt(0) == '#')
32        {
33            try
34            {
35                return convertHexEncoded(value, 1);
36            }
37            catch (IOException e)
38            {
39                throw new RuntimeException("can't recode value for oid " + oid.getId());
40            }
41        }
42        else
43        {
44            if (value.length() != 0 && value.charAt(0) == '\\')
45            {
46                value = value.substring(1);
47            }
48            if (oid.equals(X509Name.EmailAddress) || oid.equals(X509Name.DC))
49            {
50                return new DERIA5String(value);
51            }
52            else if (oid.equals(X509Name.DATE_OF_BIRTH))  // accept time string as well as # (for compatibility)
53            {
54                return new DERGeneralizedTime(value);
55            }
56            else if (oid.equals(X509Name.C) || oid.equals(X509Name.SN) || oid.equals(X509Name.DN_QUALIFIER)
57                || oid.equals(X509Name.TELEPHONE_NUMBER))
58            {
59                 return new DERPrintableString(value);
60            }
61        }
62
63        return new DERUTF8String(value);
64    }
65}
66