jdk/src/share/classes/sun/security/pkcs/PKCS9Attributes.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.pkcs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.util.DerEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.DerInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.DerOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.ObjectIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * A set of attributes of class PKCS9Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Douglas Hoover
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class PKCS9Attributes {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * Attributes in this set indexed by OID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private final Hashtable<ObjectIdentifier, PKCS9Attribute> attributes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        new Hashtable<ObjectIdentifier, PKCS9Attribute>(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * The keys of this hashtable are the OIDs of permitted attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final Hashtable<ObjectIdentifier, ObjectIdentifier> permittedAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * The DER encoding of this attribute set.  The tag byte must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * DerValue.tag_SetOf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final byte[] derEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Contols how attributes, which are not recognized by the PKCS9Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * class, are handled during parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private boolean ignoreUnsupportedAttributes = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Construct a set of PKCS9 Attributes from its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * DER encoding on a DerInputStream, accepting only attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * with OIDs on the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * list.  If the array is null, accept all attributes supported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * class PKCS9Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param permittedAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Array of attribute OIDs that will be accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * the contents of the DER encoding of the attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @exception IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * on i/o error, encoding syntax error, unacceptable or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * unsupported attribute, or duplicate attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @see PKCS9Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public PKCS9Attributes(ObjectIdentifier[] permittedAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                           DerInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (permittedAttributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            this.permittedAttributes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                new Hashtable<ObjectIdentifier, ObjectIdentifier>(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                                permittedAttributes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            for (int i = 0; i < permittedAttributes.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                this.permittedAttributes.put(permittedAttributes[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                             permittedAttributes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            this.permittedAttributes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // derEncoding initialized in <code>decode()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        derEncoding = decode(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Construct a set of PKCS9 Attributes from the contents of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * DER encoding on a DerInputStream.  Accept all attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * supported by class PKCS9Attribute and reject any unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param in the contents of the DER encoding of the attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @exception IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * on i/o error, encoding syntax error, or unsupported or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * duplicate attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @see PKCS9Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public PKCS9Attributes(DerInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this(in, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Construct a set of PKCS9 Attributes from the contents of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * DER encoding on a DerInputStream.  Accept all attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * supported by class PKCS9Attribute and ignore any unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * attributes, if directed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param in the contents of the DER encoding of the attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param ignoreUnsupportedAttributes If true then any attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * not supported by the PKCS9Attribute class are ignored. Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * unsupported attributes cause an exception to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @exception IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * on i/o error, encoding syntax error, or unsupported or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * duplicate attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @see PKCS9Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public PKCS9Attributes(DerInputStream in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        boolean ignoreUnsupportedAttributes) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.ignoreUnsupportedAttributes = ignoreUnsupportedAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // derEncoding initialized in <code>decode()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        derEncoding = decode(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        permittedAttributes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Construct a set of PKCS9 Attributes from the given array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * PKCS9 attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * DER encoding on a DerInputStream.  All attributes in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <code>attribs</code> must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * supported by class PKCS9Attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @exception IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * on i/o error, encoding syntax error, or unsupported or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * duplicate attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @see PKCS9Attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public PKCS9Attributes(PKCS9Attribute[] attribs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    throws IllegalArgumentException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        ObjectIdentifier oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        for (int i=0; i < attribs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            oid = attribs[i].getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (attributes.containsKey(oid))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                          "PKCSAttribute " + attribs[i].getOID() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                          " duplicated while constructing " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                          "PKCS9Attributes.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            attributes.put(oid, attribs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        derEncoding = generateDerEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        permittedAttributes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Decode this set of PKCS9 attributes from the contents of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * DER encoding. Ignores unsupported attributes when directed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * the contents of the DER encoding of the attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @exception IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * on i/o error, encoding syntax error, unacceptable or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * unsupported attribute, or duplicate attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private byte[] decode(DerInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        DerValue val = in.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // save the DER encoding with its proper tag byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        byte[] derEncoding = val.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        derEncoding[0] = DerValue.tag_SetOf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        DerInputStream derIn = new DerInputStream(derEncoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        DerValue[] derVals = derIn.getSet(3,true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        PKCS9Attribute attrib;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        ObjectIdentifier oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        boolean reuseEncoding = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        for (int i=0; i < derVals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                attrib = new PKCS9Attribute(derVals[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            } catch (ParsingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                if (ignoreUnsupportedAttributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    reuseEncoding = false; // cannot reuse supplied DER encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    continue; // skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            oid = attrib.getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (attributes.get(oid) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                throw new IOException("Duplicate PKCS9 attribute: " + oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (permittedAttributes != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                !permittedAttributes.containsKey(oid))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                throw new IOException("Attribute " + oid +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                      " not permitted in this attribute set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            attributes.put(oid, attrib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return reuseEncoding ? derEncoding : generateDerEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Put the DER encoding of this PKCS9 attribute set on an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * DerOutputStream, tagged with the given implicit tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param tag the implicit tag to use in the DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param out the output stream on which to put the DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @exception IOException  on output error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public void encode(byte tag, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        out.write(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        out.write(derEncoding, 1, derEncoding.length -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private byte[] generateDerEncoding() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        Object[] attribVals = attributes.values().toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        out.putOrderedSetOf(DerValue.tag_SetOf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            castToDerEncoder(attribVals));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Return the DER encoding of this attribute set, tagged with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * DerValue.tag_SetOf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public byte[] getDerEncoding() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return derEncoding.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Get an attribute from this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public PKCS9Attribute getAttribute(ObjectIdentifier oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return attributes.get(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Get an attribute from this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public PKCS9Attribute getAttribute(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return attributes.get(PKCS9Attribute.getOID(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Get an array of all attributes in this set, in order of OID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public PKCS9Attribute[] getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        PKCS9Attribute[] attribs = new PKCS9Attribute[attributes.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        ObjectIdentifier oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        for (int i=1; i < PKCS9Attribute.PKCS9_OIDS.length &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                      j < attribs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            attribs[j] = getAttribute(PKCS9Attribute.PKCS9_OIDS[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (attribs[j] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return attribs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Get an attribute value by OID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public Object getAttributeValue(ObjectIdentifier oid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            Object value = getAttribute(oid).getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } catch (NullPointerException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            throw new IOException("No value found for attribute " + oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *  Get an attribute value by type name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public Object getAttributeValue(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        ObjectIdentifier oid = PKCS9Attribute.getOID(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (oid == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            throw new IOException("Attribute name " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                  " not recognized or not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return getAttributeValue(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Returns the PKCS9 block in a printable string form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        StringBuffer buf = new StringBuffer(200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        buf.append("PKCS9 Attributes: [\n\t");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        ObjectIdentifier oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        PKCS9Attribute value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        boolean first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        for (int i = 1; i < PKCS9Attribute.PKCS9_OIDS.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            value = getAttribute(PKCS9Attribute.PKCS9_OIDS[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (value == null) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            // we have a value; print it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (first)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                buf.append(";\n\t");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            buf.append(value.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        buf.append("\n\t] (end PKCS9 Attributes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Cast an object array whose components are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * <code>DerEncoder</code>s to <code>DerEncoder[]</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    static DerEncoder[] castToDerEncoder(Object[] objs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        DerEncoder[] encoders = new DerEncoder[objs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        for (int i=0; i < encoders.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            encoders[i] = (DerEncoder) objs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return encoders;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
}