jdk/src/share/classes/sun/security/x509/AlgIdDSA.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 1996-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 sun.security.x509;
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.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.interfaces.DSAParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class identifies DSS/DSA Algorithm variants, which are distinguished
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * by using different algorithm parameters <em>P, Q, G</em>.  It uses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * NIST/IETF standard DER encoding.  These are used to implement the Digital
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Signature Standard (DSS), FIPS 186.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <P><em><b>NOTE:</b>  DSS/DSA Algorithm IDs may be created without these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * parameters.  Use of DSS/DSA in modes where parameters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * either implicit (e.g. a default applicable to a site or a larger scope),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * or are derived from some Certificate Authority's DSS certificate, is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * not supported directly.  The application is responsible for creating a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * containing the required parameters prior to using the key in cryptographic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * operations.  The follwoing is an example of how this may be done assuming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * that we have a certificate called <code>currentCert</code> which doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * contain DSS/DSA parameters and we need to derive DSS/DSA parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * from a CA's certificate called <code>caCert</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * // key containing parameters to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * DSAPublicKey cAKey = (DSAPublicKey)(caCert.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * // key without parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * DSAPublicKey nullParamsKey = (DSAPublicKey)(currentCert.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * DSAParams cAKeyParams = cAKey.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * KeyFactory kf = KeyFactory.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * DSAPublicKeySpec ks = new DSAPublicKeySpec(nullParamsKey.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *                                            cAKeyParams.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *                                            cAKeyParams.getQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *                                            cAKeyParams.getG());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * DSAPublicKey usableKey = kf.generatePublic(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * </pre></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @see java.security.interfaces.DSAParams
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @see java.security.interfaces.DSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @see java.security.KeyFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @see java.security.spec.DSAPublicKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
class AlgIdDSA extends AlgorithmId implements DSAParams
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final long serialVersionUID = 3437177836797504046L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The three unsigned integer parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private BigInteger  p , q, g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /** Returns the DSS/DSA parameter "P" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public BigInteger   getP () { return p; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /** Returns the DSS/DSA parameter "Q" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public BigInteger   getQ () { return q; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /** Returns the DSS/DSA parameter "G" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public BigInteger   getG () { return g; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Default constructor.  The OID and parameters must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * deserialized before this algorithm ID is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // XXX deprecated for general use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public AlgIdDSA () {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    AlgIdDSA (DerValue val) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        { super(val.getOID()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Construct an AlgIdDSA from an X.509 encoded byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public AlgIdDSA (byte[] encodedAlg) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        { super (new DerValue(encodedAlg).getOID()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Constructs a DSS/DSA Algorithm ID from unsigned integers that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * define the algorithm parameters.  Those integers are encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * as big-endian byte arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param p the DSS/DSA paramter "P"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param q the DSS/DSA paramter "Q"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param g the DSS/DSA paramter "G"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public AlgIdDSA (byte p [], byte q [], byte g [])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this (new BigInteger (1, p),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            new BigInteger (1, q),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            new BigInteger (1, g));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Constructs a DSS/DSA Algorithm ID from numeric parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * If all three are null, then the parameters portion of the algorithm id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * is set to null.  See note in header regarding use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param p the DSS/DSA paramter "P"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param q the DSS/DSA paramter "Q"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param g the DSS/DSA paramter "G"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public AlgIdDSA (BigInteger p, BigInteger q, BigInteger g)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        super (DSA_oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (p != null || q != null || g != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (p == null || q == null || g == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                throw new ProviderException("Invalid parameters for DSS/DSA" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                            " Algorithm ID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                this.p = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                this.q = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                this.g = g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                initializeParams ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                /* this should not happen */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                throw new ProviderException ("Construct DSS/DSA Algorithm ID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Returns "DSA", indicating the Digital Signature Algorithm (DSA) as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * defined by the Digital Signature Standard (DSS), FIPS 186.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public String getName ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        { return "DSA"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * For algorithm IDs which haven't been created from a DER encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * value, "params" must be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private void initializeParams ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        DerOutputStream out = new DerOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        out.putInteger(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        out.putInteger(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        out.putInteger(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        params = new DerValue (DerValue.tag_Sequence,out.toByteArray ());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Parses algorithm parameters P, Q, and G.  They're found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * in the "params" member, which never needs to be changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    protected void decodeParams ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (params == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new IOException("DSA alg params are null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (params.tag != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw new  IOException("DSA alg parsing error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        params.data.reset ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        this.p = params.data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        this.q = params.data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        this.g = params.data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (params.data.available () != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new IOException ("AlgIdDSA params, extra="+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                   params.data.available ());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Returns a formatted string describing the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public String toString ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        { return paramsToString (); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Returns a string describing the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    protected String paramsToString ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (params == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return " null\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                "\n    p:\n" + Debug.toHexString(p) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                "\n    q:\n" + Debug.toHexString(q) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                "\n    g:\n" + Debug.toHexString(g) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
}