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