summaryrefslogtreecommitdiffstats
path: root/base/util/src/netscape/security/pkcs/PKCS10Attribute.java
blob: a649c395a972468f7e1eb2186e69987649a689c7 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// --- 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.pkcs;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.security.cert.CertificateException;

import netscape.security.util.DerEncoder;
import netscape.security.util.DerInputStream;
import netscape.security.util.DerOutputStream;
import netscape.security.util.DerValue;
import netscape.security.util.ObjectIdentifier;
import netscape.security.x509.ACertAttrSet;
import netscape.security.x509.CertAttrSet;
import netscape.security.x509.Extensions;
import netscape.security.x509.OIDMap;

/**
 * Represent a PKCS Attribute.
 * 
 * <p>
 * Attributes are addiitonal attributes which can be inserted in a PKCS certificate request. For example a
 * "Driving License Certificate" could have the driving license number as a attribute.
 * 
 * <p>
 * Attributes are represented as a sequence of the attribute identifier (Object Identifier) and a set of DER encoded
 * attribute values. The current implementation only supports one value per attribute.
 * 
 * ASN.1 definition of Attribute:
 * 
 * <pre>
 * Attribute :: SEQUENCE {
 *    type    AttributeValue,
 *    values  SET OF AttributeValue
 * }
 * AttributeValue ::= ANY
 * </pre>
 * 
 * @author Amit Kapoor
 * @author Hemma Prafullchandra
 * @version 1.13
 */
public class PKCS10Attribute implements DerEncoder, Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 2002480042340316170L;
    protected ObjectIdentifier attributeId = null;
    protected CertAttrSet attributeValue = null;

    /**
     * Default constructor. Used only by sub-classes.
     */
    public PKCS10Attribute() {
    }

    /**
     * Constructs an attribute from a DER encoded array of bytes.
     */
    public PKCS10Attribute(DerValue derVal) throws IOException {
        if (derVal.tag != DerValue.tag_Sequence) {
            throw new IOException("Sequence tag missing for PKCS10Attribute.");
        }

        DerInputStream in = derVal.toDerInputStream();
        // Object identifier
        attributeId = in.getOID();
        // System.out.println("attribute ID in pkcs10 "+attributeId.toString());

        // Rest of the stuff is attribute value(s), wrapped in a SET.
        // For now, assume there is only one attribute value present.
        DerValue[] inAttrValues = in.getSet(1);
        int attrValueNum = inAttrValues.length;
        if (attrValueNum > 1) {
            throw new IOException("More than one value per attribute not supported");
        }

        // Read the first attribute value
        DerValue inAttrValue = inAttrValues[0];

        if (attributeId.equals(PKCS9Attribute.EXTENSION_REQUEST_OID)) {
            //pkcs9 extensionAttr
            try {
                // remove the tag
                //DerValue dv = inAttrValue.data.getDerValue();
                // hack. toDerInputStream only gives one extension.
                DerInputStream fi = new DerInputStream(inAttrValue.toByteArray());
                attributeValue = (CertAttrSet) new
                            Extensions(fi);
                //CertificateExtensions(fi);
                return;
            } catch (Exception e) {
                throw new IOException(e.toString());
            }
        }
        byte[] val = inAttrValue.toByteArray();
        Class<?>[] params = { Object.class };
        try {
            @SuppressWarnings("unchecked")
            Class<CertAttrSet> extClass = (Class<CertAttrSet>) OIDMap.getClass(attributeId);
            if (extClass != null) {
                Constructor<CertAttrSet> cons = (Constructor<CertAttrSet>) extClass.getConstructor(params);
                Object value = Array.newInstance(byte.class, val.length);
                for (int i = 0; i < val.length; i++) {
                    Array.setByte(value, i, val[i]);
                }
                Object[] passed = new Object[] { value };
                attributeValue = cons.newInstance(passed);
            } else {
                // attribute classes are usable for PKCS10 attributes. 
                // this is used where the attributes are not actual 
                // implemented extensions.
                attributeValue = new ACertAttrSet(inAttrValue);
            }
        } catch (InvocationTargetException invk) {
            throw new IOException(invk.getTargetException().getMessage());
        } catch (Exception e) {
            throw new IOException(e.toString());
        }
    }

    /**
     * Constructs an attribute from individual components of ObjectIdentifier
     * and the DER encoded value.
     * 
     * @param attributeId the ObjectIdentifier of the attribute.
     * @param attributeValue the CertAttrSet.
     */
    public PKCS10Attribute(ObjectIdentifier attributeId,
                           CertAttrSet attributeValue) {
        this.attributeId = attributeId;
        this.attributeValue = attributeValue;
    }

    /**
     * Constructs an attribute from another attribute. To be used for
     * creating decoded subclasses.
     * 
     * @param attr the attribute to create from.
     */
    public PKCS10Attribute(PKCS10Attribute attr) {
        this.attributeId = attr.attributeId;
        this.attributeValue = attr.attributeValue;
    }

    /**
     * Write the output to the DerOutputStream.
     * 
     * @param out the OutputStream to write the attribute to.
     * @exception CertificateException on certificate encoding errors.
     * @exception IOException on encoding errors.
     */
    public void encode(OutputStream out)
            throws CertificateException, IOException {
        // Encode the attribute value
        DerOutputStream outAttrValue = new DerOutputStream();
        attributeValue.encode(outAttrValue);

        // Wrap the encoded attribute value into a SET
        DerValue outAttrValueSet = new DerValue(DerValue.tag_Set,
                        outAttrValue.toByteArray());

        // Create the attribute
        DerOutputStream outAttr = new DerOutputStream();
        outAttr.putOID(attributeId);
        outAttr.putDerValue(outAttrValueSet);

        // Wrap the OID and the set of attribute values into a SEQUENCE
        DerOutputStream tmp = new DerOutputStream();
        tmp.write(DerValue.tag_Sequence, outAttr);

        // write the results to out
        out.write(tmp.toByteArray());
    }

    /**
     * DER encode this object onto an output stream.
     * Implements the <code>DerEncoder</code> interface.
     * 
     * @param out
     *            the OutputStream on which to write the DER encoding.
     * 
     * @exception IOException on encoding errors.
     */
    public void derEncode(OutputStream out) throws IOException {
        try {
            encode(out);
        } catch (CertificateException ce) {
            IOException ioe = new IOException(ce.toString());
            ioe.fillInStackTrace();
            throw ioe;
        }
    }

    /**
     * Returns the ObjectIdentifier of the attribute.
     */
    public ObjectIdentifier getAttributeId() {
        return (attributeId);
    }

    /**
     * Returns the attribute value as an byte array for further processing.
     */
    public CertAttrSet getAttributeValue() {
        return (attributeValue);
    }

    /**
     * Returns the attribute in user readable form.
     */
    public String toString() {
        String s = "AttributeId: " + attributeId.toString() + "\n";
        s += "AttributeValue: " + attributeValue.toString();

        return (s);
    }
}