home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / security / SignatureSpi.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  8.6 KB  |  245 lines

  1. /*
  2.  * @(#)SignatureSpi.java    1.4 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.   
  15. package java.security;
  16.  
  17. import java.security.spec.AlgorithmParameterSpec;
  18. import java.util.*;
  19. import java.io.*;
  20.  
  21. /**
  22.  * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
  23.  * for the <code>Signature</code> class, which is used to provide the
  24.  * functionality of a digital signature algorithm. Digital signatures are used
  25.  * for authentication and integrity assurance of digital data.
  26.  *.
  27.  * <p> All the abstract methods in this class must be implemented by each
  28.  * cryptographic service provider who wishes to supply the implementation
  29.  * of a particular signature algorithm.
  30.  *
  31.  * @author Benjamin Renaud 
  32.  *
  33.  * @version 1.4 98/03/18
  34.  *
  35.  * @see Signature
  36.  */
  37.  
  38. public abstract class SignatureSpi {
  39.  
  40.     /**
  41.      * Application-specified source of randomness. 
  42.      */
  43.     protected SecureRandom appRandom = null;
  44.  
  45.     /**
  46.      * Initializes this signature object with the specified
  47.      * public key for verification operations.
  48.      *
  49.      * @param publicKey the public key of the identity whose signature is
  50.      * going to be verified.
  51.      * 
  52.      * @exception InvalidKeyException if the key is improperly
  53.      * encoded, parameters are missing, and so on.  
  54.      */
  55.     protected abstract void engineInitVerify(PublicKey publicKey)
  56.     throws InvalidKeyException;
  57.  
  58.     /**
  59.      * Initializes this signature object with the specified
  60.      * private key for signing operations.
  61.      *
  62.      * @param privateKey the private key of the identity whose signature
  63.      * will be generated.
  64.      *
  65.      * @exception InvalidKeyException if the key is improperly
  66.      * encoded, parameters are missing, and so on. 
  67.      */
  68.     protected abstract void engineInitSign(PrivateKey privateKey)
  69.     throws InvalidKeyException;
  70.  
  71.     /**
  72.      * Initializes this signature object with the specified
  73.      * private key and source of randomness for signing operations.
  74.      *
  75.      * <p>This concrete method has been added to this previously-defined
  76.      * abstract class. (For backwards compatibility, it cannot be abstract.)
  77.      *
  78.      * @param privateKey the private key of the identity whose signature
  79.      * will be generated.
  80.      * @param random the source of randomness
  81.      *
  82.      * @exception InvalidKeyException if the key is improperly
  83.      * encoded, parameters are missing, and so on. 
  84.      */
  85.     protected void engineInitSign(PrivateKey privateKey,
  86.                   SecureRandom random)
  87.     throws InvalidKeyException {
  88.         this.appRandom = random;
  89.         engineInitSign(privateKey);
  90.     }
  91.  
  92.     /**
  93.      * Updates the data to be signed or verified
  94.      * using the specified byte.
  95.      *
  96.      * @param b the byte to use for the update.
  97.      *
  98.      * @exception SignatureException if the engine is not initialized
  99.      * properly.
  100.      */
  101.     protected abstract void engineUpdate(byte b) throws SignatureException;
  102.  
  103.     /**
  104.      * Updates the data to be signed or verified, using the 
  105.      * specified array of bytes, starting at the specified offset.
  106.      *
  107.      * @param data the array of bytes.  
  108.      * @param off the offset to start from in the array of bytes.  
  109.      * @param len the number of bytes to use, starting at offset.
  110.      *
  111.      * @exception SignatureException if the engine is not initialized 
  112.      * properly.
  113.      */
  114.     protected abstract void engineUpdate(byte[] b, int off, int len) 
  115.         throws SignatureException;
  116.  
  117.     /** 
  118.      * Returns the signature bytes of all the data
  119.      * updated so far. The signature returned is X.509-encoded.    
  120.      * For more information about the X.509 encoding, see    
  121.      * <a href = "../guide/security/cert2.html">X.509 certificates</a>.   
  122.      *
  123.      * @return the signature bytes of the signing operation's result.
  124.      *
  125.      * @exception SignatureException if the engine is not
  126.      * initialized properly.  
  127.      */
  128.     protected abstract byte[] engineSign() throws SignatureException;
  129.  
  130.     /** 
  131.      * Verifies the passed-in signature. The signature bytes 
  132.      * are expected to be X.509-encoded. For more information about the 
  133.      * X.509 encoding, see <a href = "../guide/security/cert2.html">X.509 
  134.      * certificates</a>.   
  135.      * 
  136.      * @param sigBytes the signature bytes to be verified.
  137.      *
  138.      * @return true if the signature was verified, false if not. 
  139.      *
  140.      * @exception SignatureException if the engine is not initialized 
  141.      * properly, or the passed-in signature is improperly encoded or 
  142.      * of the wrong type, etc.  
  143.      */
  144.     protected abstract boolean engineVerify(byte[] sigBytes) 
  145.     throws SignatureException;
  146.  
  147.     /**
  148.      * Sets the specified algorithm parameter to the specified
  149.      * value. This method supplies a general-purpose mechanism through
  150.      * which it is possible to set the various parameters of this object. 
  151.      * A parameter may be any settable parameter for the algorithm, such as 
  152.      * a parameter size, or a source of random bits for signature generation 
  153.      * (if appropriate), or an indication of whether or not to perform
  154.      * a specific but optional computation. A uniform algorithm-specific 
  155.      * naming scheme for each parameter is desirable but left unspecified 
  156.      * at this time.
  157.      *
  158.      * @param param the string identifier of the parameter.
  159.      *
  160.      * @param value the parameter value.
  161.      *
  162.      * @exception InvalidParameterException if <code>param</code> is an
  163.      * invalid parameter for this signature algorithm engine,
  164.      * the parameter is already set
  165.      * and cannot be set again, a security exception occurs, and so on. 
  166.      *
  167.      * @deprecated Replaced by <a href =
  168.      * "#engineSetParameter(java.security.spec.AlgorithmParameterSpec)">
  169.      * engineSetParameter</a>.
  170.      */
  171.     protected abstract void engineSetParameter(String param, Object value) 
  172.     throws InvalidParameterException;
  173.  
  174.     /**
  175.      * Initializes this signature engine with the specified parameter set.
  176.      *
  177.      * This concrete method has been added to this previously-defined
  178.      * abstract class. (For backwards compatibility, it cannot be abstract.)
  179.      * It may be overridden by a provider to set the algorithm parameters
  180.      * using the specified <code>params</code>. Such an override
  181.      * is expected to throw an InvalidAlgorithmParameterException if
  182.      * a parameter is invalid.
  183.      * If this method is not overridden, it always throws an
  184.      * UnsupportedOperationException.
  185.      *
  186.      * @param params the parameters
  187.      *
  188.      * @exception InvalidAlgorithmParameterException if the given parameters
  189.      * are inappropriate for this signature engine
  190.      */
  191.     protected void engineSetParameter(AlgorithmParameterSpec params)
  192.     throws InvalidAlgorithmParameterException {
  193.         throw new UnsupportedOperationException();
  194.     }
  195.  
  196.     /**
  197.      * Gets the value of the specified algorithm parameter. 
  198.      * This method supplies a general-purpose mechanism through which it 
  199.      * is possible to get the various parameters of this object. A parameter
  200.      * may be any settable parameter for the algorithm, such as a parameter 
  201.      * size, or  a source of random bits for signature generation (if 
  202.      * appropriate), or an indication of whether or not to perform a 
  203.      * specific but optional computation. A uniform algorithm-specific 
  204.      * naming scheme for each parameter is desirable but left unspecified 
  205.      * at this time.
  206.      *
  207.      * @param param the string name of the parameter.
  208.      *
  209.      * @return the object that represents the parameter value, or null if
  210.      * there is none.
  211.      *
  212.      * @exception InvalidParameterException if <code>param</code> is an 
  213.      * invalid parameter for this engine, or another exception occurs while
  214.      * trying to get this parameter.
  215.      *
  216.      * @deprecated
  217.      */
  218.     protected abstract Object engineGetParameter(String param)
  219.     throws InvalidParameterException;
  220.  
  221.     /**
  222.      * Returns a clone if the implementation is cloneable.
  223.      * 
  224.      * @return a clone if the implementation is cloneable.
  225.      *
  226.      * @exception CloneNotSupportedException if this is called
  227.      * on an implementation that does not support <code>Cloneable</code>.
  228.      */
  229.     public Object clone() throws CloneNotSupportedException {
  230.     if (this instanceof Cloneable) {
  231.         return super.clone();
  232.     } else {
  233.         throw new CloneNotSupportedException();
  234.     }
  235.     }
  236. }
  237.     
  238.         
  239.  
  240.  
  241.  
  242.         
  243.         
  244.     
  245.