jdk/src/share/classes/java/security/cert/PolicyQualifierInfo.java
author xuelei
Wed, 20 Jan 2010 21:38:37 +0800
changeset 4807 2521b7dcf505
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6862064: incorrect implementation of PKIXParameters.clone() Reviewed-by: weijun, mullan
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 2000-2003 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 java.security.cert;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * An immutable policy qualifier represented by the ASN.1 PolicyQualifierInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>The ASN.1 definition is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *   PolicyQualifierInfo ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *        policyQualifierId       PolicyQualifierId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *        qualifier               ANY DEFINED BY policyQualifierId }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * A certificate policies extension, if present in an X.509 version 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * certificate, contains a sequence of one or more policy information terms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * each of which consists of an object identifier (OID) and optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * qualifiers. In an end-entity certificate, these policy information terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * indicate the policy under which the certificate has been issued and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * purposes for which the certificate may be used. In a CA certificate, these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * policy information terms limit the set of policies for certification paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * which include this certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * A <code>Set</code> of <code>PolicyQualifierInfo</code> objects are returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * by the {@link PolicyNode#getPolicyQualifiers PolicyNode.getPolicyQualifiers}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * method. This allows applications with specific policy requirements to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * process and validate each policy qualifier. Applications that need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * process policy qualifiers should explicitly set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <code>policyQualifiersRejected</code> flag to false (by calling the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * {@link PKIXParameters#setPolicyQualifiersRejected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * PKIXParameters.setPolicyQualifiersRejected} method) before validating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * a certification path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>Note that the PKIX certification path validation algorithm specifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * that any policy qualifier in a certificate policies extension that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * marked critical must be processed and validated. Otherwise the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * certification path must be rejected. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <code>policyQualifiersRejected</code> flag is set to false, it is up to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * the application to validate all policy qualifiers in this manner in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * to be PKIX compliant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p><b>Concurrent Access</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>All <code>PolicyQualifierInfo</code> objects must be immutable and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * thread-safe. That is, multiple threads may concurrently invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * methods defined in this class on a single <code>PolicyQualifierInfo</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * object (or more than one) with no ill effects. Requiring
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <code>PolicyQualifierInfo</code> objects to be immutable and thread-safe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * allows them to be passed around to various pieces of code without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * worrying about coordinating access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @author      seth proctor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @author      Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
public class PolicyQualifierInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private byte [] mEncoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private String mId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private byte [] mData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private String pqiString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Creates an instance of <code>PolicyQualifierInfo</code> from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * encoded bytes. The encoded byte array is copied on construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param encoded a byte array containing the qualifier in DER encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @exception IOException thrown if the byte array does not represent a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * valid and parsable policy qualifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public PolicyQualifierInfo(byte[] encoded) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        mEncoded = encoded.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        DerValue val = new DerValue(mEncoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (val.tag != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new IOException("Invalid encoding for PolicyQualifierInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        mId = (val.data.getDerValue()).getOID().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        byte [] tmp = val.data.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (tmp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            mData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            mData = new byte[tmp.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            System.arraycopy(tmp, 0, mData, 0, tmp.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Returns the <code>policyQualifierId</code> field of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <code>PolicyQualifierInfo</code>. The <code>policyQualifierId</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * is an Object Identifier (OID) represented by a set of nonnegative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * integers separated by periods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @return the OID (never <code>null</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public final String getPolicyQualifierId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return mId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Returns the ASN.1 DER encoded form of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <code>PolicyQualifierInfo</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @return the ASN.1 DER encoded bytes (never <code>null</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Note that a copy is returned, so the data is cloned each time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public final byte[] getEncoded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return mEncoded.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Returns the ASN.1 DER encoded form of the <code>qualifier</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * field of this <code>PolicyQualifierInfo</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @return the ASN.1 DER encoded bytes of the <code>qualifier</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * field. Note that a copy is returned, so the data is cloned each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * time this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public final byte[] getPolicyQualifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return (mData == null ? null : mData.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Return a printable representation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <code>PolicyQualifierInfo</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return a <code>String</code> describing the contents of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *         <code>PolicyQualifierInfo</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (pqiString != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return pqiString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        HexDumpEncoder enc = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        sb.append("PolicyQualifierInfo: [\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        sb.append("  qualifierID: " + mId + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        sb.append("  qualifier: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            (mData == null ? "null" : enc.encodeBuffer(mData)) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        sb.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        pqiString = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return pqiString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}