1/*
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.security.pkcs10;
27
28import java.io.OutputStream;
29import java.io.IOException;
30
31import sun.security.pkcs.PKCS9Attribute;
32import sun.security.util.*;
33
34/**
35 * Represent a PKCS#10 Attribute.
36 *
37 * <p>Attributes are additonal information which can be inserted in a PKCS#10
38 * certificate request. For example a "Driving License Certificate" could have
39 * the driving license number as an attribute.
40 *
41 * <p>Attributes are represented as a sequence of the attribute identifier
42 * (Object Identifier) and a set of DER encoded attribute values.
43 *
44 * ASN.1 definition of Attribute:
45 * <pre>
46 * Attribute :: SEQUENCE {
47 *    type    AttributeType,
48 *    values  SET OF AttributeValue
49 * }
50 * AttributeType  ::= OBJECT IDENTIFIER
51 * AttributeValue ::= ANY defined by type
52 * </pre>
53 *
54 * @author Amit Kapoor
55 * @author Hemma Prafullchandra
56 */
57public class PKCS10Attribute implements DerEncoder {
58
59    protected ObjectIdentifier  attributeId = null;
60    protected Object            attributeValue = null;
61
62    /**
63     * Constructs an attribute from a DER encoding.
64     * This constructor expects the value to be encoded as defined above,
65     * i.e. a SEQUENCE of OID and SET OF value(s), not a literal
66     * X.509 v3 extension. Only PKCS9 defined attributes are supported
67     * currently.
68     *
69     * @param derVal the der encoded attribute.
70     * @exception IOException on parsing errors.
71     */
72    public PKCS10Attribute(DerValue derVal) throws IOException {
73        PKCS9Attribute attr = new PKCS9Attribute(derVal);
74        this.attributeId = attr.getOID();
75        this.attributeValue = attr.getValue();
76    }
77
78    /**
79     * Constructs an attribute from individual components of
80     * ObjectIdentifier and the value (any java object).
81     *
82     * @param attributeId the ObjectIdentifier of the attribute.
83     * @param attributeValue an instance of a class that implements
84     * the attribute identified by the ObjectIdentifier.
85     */
86    public PKCS10Attribute(ObjectIdentifier attributeId,
87                           Object attributeValue) {
88        this.attributeId = attributeId;
89        this.attributeValue = attributeValue;
90    }
91
92    /**
93     * Constructs an attribute from PKCS9 attribute.
94     *
95     * @param attr the PKCS9Attribute to create from.
96     */
97    public PKCS10Attribute(PKCS9Attribute attr) {
98        this.attributeId = attr.getOID();
99        this.attributeValue = attr.getValue();
100    }
101
102    /**
103     * DER encode this object onto an output stream.
104     * Implements the <code>DerEncoder</code> interface.
105     *
106     * @param out
107     * the OutputStream on which to write the DER encoding.
108     *
109     * @exception IOException on encoding errors.
110     */
111    public void derEncode(OutputStream out) throws IOException {
112        PKCS9Attribute attr = new PKCS9Attribute(attributeId, attributeValue);
113        attr.derEncode(out);
114    }
115
116    /**
117     * Returns the ObjectIdentifier of the attribute.
118     */
119    public ObjectIdentifier getAttributeId() {
120        return (attributeId);
121    }
122
123    /**
124     * Returns the attribute value.
125     */
126    public Object getAttributeValue() {
127        return (attributeValue);
128    }
129
130    /**
131     * Returns the attribute in user readable form.
132     */
133    public String toString() {
134        return (attributeValue.toString());
135    }
136}
137