jdk/src/share/classes/sun/security/x509/X500Signer.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
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.security.Signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.Signer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class provides a binding between a Signature object and an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * authenticated X.500 name (from an X.509 certificate chain), which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * is needed in many public key signing applications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <P>The name of the signer is important, both because knowing it is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * whole point of the signature, and because the associated X.509 certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * is always used to verify the signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <P><em>The X.509 certificate chain is temporarily not associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the signer, but this omission will be resolved.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public final class X500Signer extends Signer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final long serialVersionUID = -8609982645394364834L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Called for each chunk of the data being signed.  That
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * is, you can present the data in many chunks, so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * it doesn't need to be in a single sequential buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param buf buffer holding the next chunk of the data to be signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param offset starting point of to-be-signed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param len how many bytes of data are to be signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @exception SignatureException on errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public void update(byte buf[], int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        sig.update (buf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Produces the signature for the data processed by update().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @exception SignatureException on errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public byte[] sign() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return sig.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Returns the algorithm used to sign.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public AlgorithmId  getAlgorithmId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return algid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Returns the name of the signing agent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public X500Name     getSigner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return agent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Constructs a binding between a signature and an X500 name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * from an X.509 certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // package private  ----hmmmmm ?????
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public X500Signer(Signature sig, X500Name agent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (sig == null || agent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new IllegalArgumentException ("null parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.sig = sig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.agent = agent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
          this.algid = AlgorithmId.getAlgorithmId(sig.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new RuntimeException("internal error! " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private Signature   sig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private X500Name    agent;          // XXX should be X509CertChain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private AlgorithmId algid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}