jdk/src/share/classes/sun/security/pkcs/PKCS9Attribute.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.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.x509.CertificateExtensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.DerEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.util.DerInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.util.DerOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.util.ObjectIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Class supporting any PKCS9 attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Supports DER decoding and access to attribute values, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * DER encoding or setting of values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <a name="classTable"><h3>Type/Class Table</h3></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The following table shows the correspondence between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * PKCS9 attribute types and value component classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <TABLE BORDER CELLPADDING=8 ALIGN=CENTER>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <TH>Object Identifier</TH>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <TH>Attribute Name</TH>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <TH>Type</TH>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <TH>Value Class</TH>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <TD>1.2.840.113549.1.9.1</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <TD>EmailAddress</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <TD>Multi-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <TD><code>String[]</code></TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <TD>1.2.840.113549.1.9.2</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <TD>UnstructuredName</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <TD>Multi-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <TD><code>String[]</code></TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <TD>1.2.840.113549.1.9.3</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <TD>ContentType</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <TD>Single-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <TD><code>ObjectIdentifier</code></TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <TD>1.2.840.113549.1.9.4</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <TD>MessageDigest</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <TD>Single-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <TD><code>byte[]</code></TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <TD>1.2.840.113549.1.9.5</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <TD>SigningTime</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <TD>Single-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <TD><code>Date</code></TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <TD>1.2.840.113549.1.9.6</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <TD>Countersignature</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <TD>Multi-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <TD><code>SignerInfo[]</code></TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <TD>1.2.840.113549.1.9.7</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <TD>ChallengePassword</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <TD>Single-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <TD><code>String</code></TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <TD>1.2.840.113549.1.9.8</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <TD>UnstructuredAddress</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <TD>Single-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <TD><code>String</code></TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <TD>1.2.840.113549.1.9.9</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <TD>ExtendedCertificateAttributes</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <TD>Multi-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * <TD>(not supported)</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <TD>1.2.840.113549.1.9.10</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <TD>IssuerAndSerialNumber</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <TD>Single-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <TD>(not supported)</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * <TD>1.2.840.113549.1.9.{11,12}</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <TD>RSA DSI proprietary</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <TD>Single-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <TD>(not supported)</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <TD>1.2.840.113549.1.9.13</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * <TD>S/MIME unused assignment</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * <TD>Single-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <TD>(not supported)</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <TD>1.2.840.113549.1.9.14</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <TD>ExtensionRequest</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <TD>Single-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <TD>CertificateExtensions</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <TD>1.2.840.113549.1.9.15</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * <TD>SMIMECapability</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * <TD>Single-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <TD>(not supported)</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * <TD>1.2.840.113549.1.9.16.2.12</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * <TD>SigningCertificate</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * <TD>Single-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * <TD>SigningCertificateInfo</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * <TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * <TD>1.2.840.113549.1.9.16.2.14</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * <TD>SignatureTimestampToken</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * <TD>Single-valued</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * <TD>byte[]</TD>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * </TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * </TABLE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * @author Douglas Hoover
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
public class PKCS9Attribute implements DerEncoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /* Are we debugging ? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private static final Debug debug = Debug.getInstance("jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Array of attribute OIDs defined in PKCS9, by number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    static final ObjectIdentifier[] PKCS9_OIDS = new ObjectIdentifier[18];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    static {   // static initializer for PKCS9_OIDS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        for (int i = 1; i < PKCS9_OIDS.length - 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            PKCS9_OIDS[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                ObjectIdentifier.newInternal(new int[]{1,2,840,113549,1,9,i});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // Initialize SigningCertificate and SignatureTimestampToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // separately (because their values are out of sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        PKCS9_OIDS[PKCS9_OIDS.length - 2] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            ObjectIdentifier.newInternal(new int[]{1,2,840,113549,1,9,16,2,12});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        PKCS9_OIDS[PKCS9_OIDS.length - 1] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            ObjectIdentifier.newInternal(new int[]{1,2,840,113549,1,9,16,2,14});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    // first element [0] not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public static final ObjectIdentifier EMAIL_ADDRESS_OID = PKCS9_OIDS[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public static final ObjectIdentifier UNSTRUCTURED_NAME_OID = PKCS9_OIDS[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public static final ObjectIdentifier CONTENT_TYPE_OID = PKCS9_OIDS[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public static final ObjectIdentifier MESSAGE_DIGEST_OID = PKCS9_OIDS[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public static final ObjectIdentifier SIGNING_TIME_OID = PKCS9_OIDS[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public static final ObjectIdentifier COUNTERSIGNATURE_OID = PKCS9_OIDS[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public static final ObjectIdentifier CHALLENGE_PASSWORD_OID = PKCS9_OIDS[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public static final ObjectIdentifier UNSTRUCTURED_ADDRESS_OID = PKCS9_OIDS[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public static final ObjectIdentifier EXTENDED_CERTIFICATE_ATTRIBUTES_OID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                         = PKCS9_OIDS[9];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public static final ObjectIdentifier ISSUER_SERIALNUMBER_OID = PKCS9_OIDS[10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    // [11], [12] are RSA DSI proprietary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    // [13] ==> signingDescription, S/MIME, not used anymore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public static final ObjectIdentifier EXTENSION_REQUEST_OID = PKCS9_OIDS[14];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public static final ObjectIdentifier SMIME_CAPABILITY_OID = PKCS9_OIDS[15];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public static final ObjectIdentifier SIGNING_CERTIFICATE_OID = PKCS9_OIDS[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public static final ObjectIdentifier SIGNATURE_TIMESTAMP_TOKEN_OID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                PKCS9_OIDS[17];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public static final String EMAIL_ADDRESS_STR = "EmailAddress";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public static final String UNSTRUCTURED_NAME_STR = "UnstructuredName";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public static final String CONTENT_TYPE_STR = "ContentType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public static final String MESSAGE_DIGEST_STR = "MessageDigest";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public static final String SIGNING_TIME_STR = "SigningTime";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public static final String COUNTERSIGNATURE_STR = "Countersignature";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public static final String CHALLENGE_PASSWORD_STR = "ChallengePassword";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public static final String UNSTRUCTURED_ADDRESS_STR = "UnstructuredAddress";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public static final String EXTENDED_CERTIFICATE_ATTRIBUTES_STR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                               "ExtendedCertificateAttributes";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public static final String ISSUER_SERIALNUMBER_STR = "IssuerAndSerialNumber";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    // [11], [12] are RSA DSI proprietary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private static final String RSA_PROPRIETARY_STR = "RSAProprietary";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    // [13] ==> signingDescription, S/MIME, not used anymore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private static final String SMIME_SIGNING_DESC_STR = "SMIMESigningDesc";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public static final String EXTENSION_REQUEST_STR = "ExtensionRequest";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public static final String SMIME_CAPABILITY_STR = "SMIMECapability";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public static final String SIGNING_CERTIFICATE_STR = "SigningCertificate";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public static final String SIGNATURE_TIMESTAMP_TOKEN_STR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                "SignatureTimestampToken";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Hashtable mapping names and variant names of supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * attributes to their OIDs. This table contains all name forms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * that occur in PKCS9, in lower case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private static final Hashtable<String, ObjectIdentifier> NAME_OID_TABLE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        new Hashtable<String, ObjectIdentifier>(18);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    static { // static initializer for PCKS9_NAMES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        NAME_OID_TABLE.put("emailaddress", PKCS9_OIDS[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        NAME_OID_TABLE.put("unstructuredname", PKCS9_OIDS[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        NAME_OID_TABLE.put("contenttype", PKCS9_OIDS[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        NAME_OID_TABLE.put("messagedigest", PKCS9_OIDS[4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        NAME_OID_TABLE.put("signingtime", PKCS9_OIDS[5]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        NAME_OID_TABLE.put("countersignature", PKCS9_OIDS[6]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        NAME_OID_TABLE.put("challengepassword", PKCS9_OIDS[7]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        NAME_OID_TABLE.put("unstructuredaddress", PKCS9_OIDS[8]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        NAME_OID_TABLE.put("extendedcertificateattributes", PKCS9_OIDS[9]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        NAME_OID_TABLE.put("issuerandserialnumber", PKCS9_OIDS[10]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        NAME_OID_TABLE.put("rsaproprietary", PKCS9_OIDS[11]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        NAME_OID_TABLE.put("rsaproprietary", PKCS9_OIDS[12]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        NAME_OID_TABLE.put("signingdescription", PKCS9_OIDS[13]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        NAME_OID_TABLE.put("extensionrequest", PKCS9_OIDS[14]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        NAME_OID_TABLE.put("smimecapability", PKCS9_OIDS[15]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        NAME_OID_TABLE.put("signingcertificate", PKCS9_OIDS[16]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        NAME_OID_TABLE.put("signaturetimestamptoken", PKCS9_OIDS[17]);
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
     * Hashtable mapping attribute OIDs defined in PKCS9 to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * corresponding attribute value type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private static final Hashtable<ObjectIdentifier, String> OID_NAME_TABLE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        new Hashtable<ObjectIdentifier, String>(16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        OID_NAME_TABLE.put(PKCS9_OIDS[1], EMAIL_ADDRESS_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        OID_NAME_TABLE.put(PKCS9_OIDS[2], UNSTRUCTURED_NAME_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        OID_NAME_TABLE.put(PKCS9_OIDS[3], CONTENT_TYPE_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        OID_NAME_TABLE.put(PKCS9_OIDS[4], MESSAGE_DIGEST_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        OID_NAME_TABLE.put(PKCS9_OIDS[5], SIGNING_TIME_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        OID_NAME_TABLE.put(PKCS9_OIDS[6], COUNTERSIGNATURE_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        OID_NAME_TABLE.put(PKCS9_OIDS[7], CHALLENGE_PASSWORD_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        OID_NAME_TABLE.put(PKCS9_OIDS[8], UNSTRUCTURED_ADDRESS_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        OID_NAME_TABLE.put(PKCS9_OIDS[9], EXTENDED_CERTIFICATE_ATTRIBUTES_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        OID_NAME_TABLE.put(PKCS9_OIDS[10], ISSUER_SERIALNUMBER_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        OID_NAME_TABLE.put(PKCS9_OIDS[11], RSA_PROPRIETARY_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        OID_NAME_TABLE.put(PKCS9_OIDS[12], RSA_PROPRIETARY_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        OID_NAME_TABLE.put(PKCS9_OIDS[13], SMIME_SIGNING_DESC_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        OID_NAME_TABLE.put(PKCS9_OIDS[14], EXTENSION_REQUEST_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        OID_NAME_TABLE.put(PKCS9_OIDS[15], SMIME_CAPABILITY_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        OID_NAME_TABLE.put(PKCS9_OIDS[16], SIGNING_CERTIFICATE_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        OID_NAME_TABLE.put(PKCS9_OIDS[17], SIGNATURE_TIMESTAMP_TOKEN_STR);
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
     * Acceptable ASN.1 tags for DER encodings of values of PKCS9
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * attributes, by index in <code>PKCS9_OIDS</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Sets of acceptable tags are represented as arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    private static final Byte[][] PKCS9_VALUE_TAGS = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        {new Byte(DerValue.tag_IA5String)},   // EMailAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        {new Byte(DerValue.tag_IA5String)},   // UnstructuredName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        {new Byte(DerValue.tag_ObjectId)},    // ContentType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        {new Byte(DerValue.tag_OctetString)}, // MessageDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        {new Byte(DerValue.tag_UtcTime)},     // SigningTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        {new Byte(DerValue.tag_Sequence)},    // Countersignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        {new Byte(DerValue.tag_PrintableString),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         new Byte(DerValue.tag_T61String)},   // ChallengePassword
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        {new Byte(DerValue.tag_PrintableString),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         new Byte(DerValue.tag_T61String)},   // UnstructuredAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        {new Byte(DerValue.tag_SetOf)},       // ExtendedCertificateAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        {new Byte(DerValue.tag_Sequence)},    // issuerAndSerialNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        {new Byte(DerValue.tag_Sequence)},    // extensionRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        {new Byte(DerValue.tag_Sequence)},    // SMIMECapability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        {new Byte(DerValue.tag_Sequence)},    // SigningCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        {new Byte(DerValue.tag_Sequence)}     // SignatureTimestampToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    private static final Class[] VALUE_CLASSES = new Class[18];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            Class str = Class.forName("[Ljava.lang.String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            VALUE_CLASSES[0] = null;  // not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            VALUE_CLASSES[1] = str;   // EMailAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            VALUE_CLASSES[2] = str;   // UnstructuredName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            VALUE_CLASSES[3] =        // ContentType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                Class.forName("sun.security.util.ObjectIdentifier");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            VALUE_CLASSES[4] = Class.forName("[B"); // MessageDigest (byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            VALUE_CLASSES[5] = Class.forName("java.util.Date"); // SigningTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            VALUE_CLASSES[6] =        // Countersignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                Class.forName("[Lsun.security.pkcs.SignerInfo;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            VALUE_CLASSES[7] =        // ChallengePassword
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                Class.forName("java.lang.String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            VALUE_CLASSES[8] = str;   // UnstructuredAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            VALUE_CLASSES[9] = null;  // ExtendedCertificateAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            VALUE_CLASSES[10] = null;  // IssuerAndSerialNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            VALUE_CLASSES[11] = null;  // not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            VALUE_CLASSES[12] = null;  // not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            VALUE_CLASSES[13] = null;  // not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            VALUE_CLASSES[14] =        // ExtensionRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                Class.forName("sun.security.x509.CertificateExtensions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            VALUE_CLASSES[15] = null;  // not supported yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            VALUE_CLASSES[16] = null;  // not supported yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            VALUE_CLASSES[17] = Class.forName("[B");  // SignatureTimestampToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            throw new ExceptionInInitializerError(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Array indicating which PKCS9 attributes are single-valued,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * by index in <code>PKCS9_OIDS</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    private static final boolean[] SINGLE_VALUED = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
      false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
      false,   // EMailAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
      false,   // UnstructuredName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
      true,    // ContentType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
      true,    // MessageDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
      true,    // SigningTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
      false,   // Countersignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
      true,    // ChallengePassword
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
      false,   // UnstructuredAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
      false,   // ExtendedCertificateAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
      true,    // IssuerAndSerialNumber - not supported yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
      false,   // not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
      false,   // not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
      false,   // not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
      true,    // ExtensionRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
      true,    // SMIMECapability - not supported yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
      true,    // SigningCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
      true     // SignatureTimestampToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * The OID of this attribute is <code>PKCS9_OIDS[index]</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    private int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Value set of this attribute.  Its class is given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <code>VALUE_CLASSES[index]</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    private Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Construct an attribute object from the attribute's OID and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * value.  If the attribute is single-valued, provide only one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * value.  If the attribute is multi-valued, provide an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * containing all the values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Arrays of length zero are accepted, though probably useless.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * <P> The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * <a href=#classTable>table</a> gives the class that <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * must have for a given attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public PKCS9Attribute(ObjectIdentifier oid, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        init(oid, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Construct an attribute object from the attribute's name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * value.  If the attribute is single-valued, provide only one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * value.  If the attribute is multi-valued, provide an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * containing all the values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Arrays of length zero are accepted, though probably useless.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <P> The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * <a href=#classTable>table</a> gives the class that <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * must have for a given attribute. Reasonable variants of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * attributes are accepted; in particular, case does not matter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @exception IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * if the <code>name</code> is not recognized of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * <code>value</code> has the wrong type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public PKCS9Attribute(String name, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        ObjectIdentifier oid = getOID(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (oid == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                       "Unrecognized attribute name " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                       " constructing PKCS9Attribute.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        init(oid, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    private void init(ObjectIdentifier oid, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        index = indexOf(oid, PKCS9_OIDS, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        if (index == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                       "Unsupported OID " + oid +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                       " constructing PKCS9Attribute.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (!VALUE_CLASSES[index].isInstance(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                           "Wrong value class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                           " for attribute " + oid +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                           " constructing PKCS9Attribute; was " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                           value.getClass().toString() + ", should be " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                           VALUE_CLASSES[index].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Construct a PKCS9Attribute from its encoding on an input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @param val the DerValue representing the DER encoding of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @exception IOException on parsing error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public PKCS9Attribute(DerValue derVal) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        DerInputStream derIn = new DerInputStream(derVal.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        DerValue[] val =  derIn.getSequence(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        if (derIn.available() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            throw new IOException("Excess data parsing PKCS9Attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (val.length != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            throw new IOException("PKCS9Attribute doesn't have two components");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        // get the oid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        ObjectIdentifier oid = val[0].getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        index = indexOf(oid, PKCS9_OIDS, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                debug.println("ignoring unsupported signer attribute: " + oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            throw new ParsingException("Unsupported PKCS9 attribute: " + oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        DerValue[] elems = new DerInputStream(val[1].toByteArray()).getSet(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        // check single valued have only one value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if (SINGLE_VALUED[index] && elems.length > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            throwSingleValuedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        // check for illegal element tags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        Byte tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        for (int i=0; i < elems.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            tag = new Byte(elems[i].tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            if (indexOf(tag, PKCS9_VALUE_TAGS[index], 0) == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                throwTagException(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        switch (index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        case 1:     // email address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        case 2:     // unstructured name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        case 8:     // unstructured address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            { // open scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                String[] values = new String[elems.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                for (int i=0; i < elems.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    values[i] = elems[i].getAsString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                value = values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            } // close scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        case 3:     // content type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            value = elems[0].getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        case 4:     // message digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            value = elems[0].getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        case 5:     // signing time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            value = (new DerInputStream(elems[0].toByteArray())).getUTCTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        case 6:     // countersignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            { // open scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                SignerInfo[] values = new SignerInfo[elems.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                for (int i=0; i < elems.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    values[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                        new SignerInfo(elems[i].toDerInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                value = values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            } // close scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        case 7:     // challenge password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            value = elems[0].getAsString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        case 9:     // extended-certificate attribute -- not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            throw new IOException("PKCS9 extended-certificate " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                                  "attribute not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            // break unnecessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        case 10:    // issuerAndserialNumber attribute -- not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            throw new IOException("PKCS9 IssuerAndSerialNumber" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                                  "attribute not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            // break unnecessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        case 11:    // RSA DSI proprietary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        case 12:    // RSA DSI proprietary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            throw new IOException("PKCS9 RSA DSI attributes" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                                  "11 and 12, not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            // break unnecessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        case 13:    // S/MIME unused attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            throw new IOException("PKCS9 attribute #13 not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            // break unnecessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        case 14:     // ExtensionRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            value = new CertificateExtensions(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                       new DerInputStream(elems[0].toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        case 15:     // SMIME-capability attribute -- not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            throw new IOException("PKCS9 SMIMECapability " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                                  "attribute not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            // break unnecessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        case 16:     // SigningCertificate attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            value = new SigningCertificateInfo(elems[0].toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        case 17:     // SignatureTimestampToken attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            value = elems[0].toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        default: // can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Write the DER encoding of this attribute to an output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <P> N.B.: This method always encodes values of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * ChallengePassword and UnstructuredAddress attributes as ASN.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * <code>PrintableString</code>s, without checking whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * should be encoded as <code>T61String</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    public void derEncode(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        temp.putOID(getOID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        switch (index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        case 1:     // email address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        case 2:     // unstructured name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            { // open scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                String[] values = (String[]) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                DerOutputStream[] temps = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    DerOutputStream[values.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                for (int i=0; i < values.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    temps[i] = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    temps[i].putIA5String( values[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                temp.putOrderedSetOf(DerValue.tag_Set, temps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            } // close scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        case 3:     // content type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                DerOutputStream temp2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                temp2.putOID((ObjectIdentifier) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                temp.write(DerValue.tag_Set, temp2.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        case 4:     // message digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                DerOutputStream temp2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                temp2.putOctetString((byte[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                temp.write(DerValue.tag_Set, temp2.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        case 5:     // signing time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                DerOutputStream temp2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                temp2.putUTCTime((Date) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                temp.write(DerValue.tag_Set, temp2.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        case 6:     // countersignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            temp.putOrderedSetOf(DerValue.tag_Set, (DerEncoder[]) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        case 7:     // challenge password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                DerOutputStream temp2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                temp2.putPrintableString((String) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                temp.write(DerValue.tag_Set, temp2.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        case 8:     // unstructured address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            { // open scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                String[] values = (String[]) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                DerOutputStream[] temps = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    DerOutputStream[values.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                for (int i=0; i < values.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    temps[i] = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    temps[i].putPrintableString(values[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                temp.putOrderedSetOf(DerValue.tag_Set, temps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            } // close scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        case 9:     // extended-certificate attribute -- not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            throw new IOException("PKCS9 extended-certificate " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                                  "attribute not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            // break unnecessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        case 10:    // issuerAndserialNumber attribute -- not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            throw new IOException("PKCS9 IssuerAndSerialNumber" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                                  "attribute not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            // break unnecessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        case 11:    // RSA DSI proprietary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        case 12:    // RSA DSI proprietary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            throw new IOException("PKCS9 RSA DSI attributes" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                                  "11 and 12, not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            // break unnecessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        case 13:    // S/MIME unused attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            throw new IOException("PKCS9 attribute #13 not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            // break unnecessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        case 14:     // ExtensionRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                DerOutputStream temp2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                CertificateExtensions exts = (CertificateExtensions)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    exts.encode(temp2, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                } catch (CertificateException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                    throw new IOException(ex.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                temp.write(DerValue.tag_Set, temp2.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        case 15:    // SMIMECapability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            throw new IOException("PKCS9 attribute #15 not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            // break unnecessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        case 16:    // SigningCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                "PKCS9 SigningCertificate attribute not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            // break unnecessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        case 17:    // SignatureTimestampToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            temp.write(DerValue.tag_Set, (byte[])value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        default: // can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        DerOutputStream derOut = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        derOut.write(DerValue.tag_Sequence, temp.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        out.write(derOut.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * Get the value of this attribute.  If the attribute is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * single-valued, return just the one value.  If the attribute is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * multi-valued, return an array containing all the values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * It is possible for this array to be of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * <P> The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * <a href=#classTable>table</a> gives the class of the value returned,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * depending on the type of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    public Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * Show whether this attribute is single-valued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    public boolean isSingleValued() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        return SINGLE_VALUED[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *  Return the OID of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    public ObjectIdentifier getOID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        return PKCS9_OIDS[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *  Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        return OID_NAME_TABLE.get(PKCS9_OIDS[index]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * Return the OID for a given attribute name or null if we don't recognize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    public static ObjectIdentifier getOID(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        return NAME_OID_TABLE.get(name.toLowerCase());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * Return the attribute name for a given OID or null if we don't recognize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * the oid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    public static String getName(ObjectIdentifier oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        return OID_NAME_TABLE.get(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * Returns a string representation of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        StringBuffer buf = new StringBuffer(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        buf.append("[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        buf.append(OID_NAME_TABLE.get(PKCS9_OIDS[index]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        buf.append(": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        if (SINGLE_VALUED[index]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            if (value instanceof byte[]) { // special case for octet string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                HexDumpEncoder hexDump = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                buf.append(hexDump.encodeBuffer((byte[]) value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                buf.append(value.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            buf.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        } else { // multi-valued
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            boolean first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            Object[] values = (Object[]) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            for (int j=0; j < values.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                if (first)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                    buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                buf.append(values[j].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * Beginning the search at <code>start</code>, find the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * index <code>i</code> such that <code>a[i] = obj</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * @return the index, if found, and -1 otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    static int indexOf(Object obj, Object[] a, int start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        for (int i=start; i < a.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            if (obj.equals(a[i])) return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * Throw an exception when there are multiple values for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * a single-valued attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    private void throwSingleValuedException() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        throw new IOException("Single-value attribute " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                              getOID() + " (" + getName() + ")" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                              " has multiple values.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * Throw an exception when the tag on a value encoding is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * wrong for the attribute whose value it is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    private void throwTagException(Byte tag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        Byte[] expectedTags = PKCS9_VALUE_TAGS[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        StringBuffer msg = new StringBuffer(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        msg.append("Value of attribute ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        msg.append(getOID().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        msg.append(" (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        msg.append(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        msg.append(") has wrong tag: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        msg.append(tag.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        msg.append(".  Expected tags: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        msg.append(expectedTags[0].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        for (int i = 1; i < expectedTags.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            msg.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            msg.append(expectedTags[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        msg.append(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        throw new IOException(msg.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
}