summaryrefslogtreecommitdiffstats
path: root/base/util/src/netscape/security/x509/PrintableConverter.java
blob: d63696d8f70310b50cef0867021de010d7f8aedd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package netscape.security.x509;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetEncoder;

import netscape.security.util.ASN1CharStrConvMap;
import netscape.security.util.DerValue;

/**
 * A AVAValueConverter that converts a Printable String attribute to a DerValue
 * and vice versa. An example an attribute that is a printable string is "C".
 * 
 * @see ASN1CharStrConvMap
 * @see AVAValueConverter
 * 
 * @author Lily Hsiao, Slava Galperin at Netscape Communications, Inc.
 */

public class PrintableConverter implements AVAValueConverter {
    // public constructors.

    public PrintableConverter() {
    }

    /**
     * Converts a string with ASN.1 Printable characters to a DerValue.
     * 
     * @param valueString a string with Printable characters.
     * 
     * @return a DerValue.
     * 
     * @exception IOException if a Printable encoder is not
     *                available for the conversion.
     */
    public DerValue getValue(String valueString)
            throws IOException {
        return getValue(valueString, null);
    }

    public DerValue getValue(String valueString, byte[] tags) throws IOException {
        try {
            CharsetEncoder encoder = ASN1CharStrConvMap.getDefault().getEncoder(DerValue.tag_PrintableString);
            if (encoder == null)
                throw new IOException("No encoder for printable");

            CharBuffer charBuffer = CharBuffer.wrap(valueString.toCharArray());
            ByteBuffer byteBuffer = encoder.encode(charBuffer);

            return new DerValue(DerValue.tag_PrintableString,
                    byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit());

        } catch (CharacterCodingException e) {
            throw new IllegalArgumentException("Invalid Printable String AVA Value", e);
        }
    }

    /**
     * Converts a BER encoded value of PrintableString to a DER encoded value.
     * Checks if the BER encoded value is a PrintableString.
     * NOTE only DER encoded values are currently accepted on input.
     * 
     * @param berStream A byte array of the BER encoded value.
     * 
     * @return A DerValue.
     * 
     * @exception IOException if the BER value cannot be converted to a
     *                PrintableString DER value.
     */
    public DerValue getValue(byte[] berStream)
            throws IOException {
        DerValue value = new DerValue(berStream);
        if (value.tag != DerValue.tag_PrintableString)
            throw new IOException("Invalid Printable String AVA Value");
        return value;
    }

    /**
     * Converts a DerValue of PrintableString to a java string with
     * PrintableString characters.
     * 
     * @param avaValue a DerValue.
     * 
     * @return a string with PrintableString characters.
     * 
     * @exception IOException if the DerValue is not a PrintableString i.e.
     *                The DerValue cannot be converted to a string
     *                with PrintableString characters.
     */
    public String getAsString(DerValue avaValue)
            throws IOException {
        return avaValue.getPrintableString();
    }

}