home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Signer.java < prev    next >
Text File  |  1997-05-20  |  4KB  |  133 lines

  1. /*
  2.  * @(#)Signer.java    1.22 97/01/31
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.security;
  24.  
  25. import java.io.*;
  26.  
  27. /**
  28.  * This class is used to represent an Identity that can also digitally
  29.  * sign data.
  30.  *
  31.  * <p>The management of a signer's private keys is an important and
  32.  * sensitive issue that should be handled by subclasses as appropriate
  33.  * to their intended use.
  34.  *
  35.  * @see Identity
  36.  *
  37.  * @version     1.22, 01/31/97
  38.  * @author Benjamin Renaud 
  39.  */
  40. public abstract class Signer extends Identity {
  41.  
  42.     private PrivateKey privateKey;
  43.  
  44.     /** 
  45.      * Creates a signer. This constructor should only be used for 
  46.      * serialization. 
  47.      */      
  48.     protected Signer() {
  49.     super();
  50.     }
  51.  
  52.  
  53.     /** 
  54.      * Creates a signer with the specified identity name.
  55.      * 
  56.      * @param name the identity name.   
  57.      */
  58.     public Signer(String name) {
  59.     super(name);
  60.     }
  61.     
  62.     /** 
  63.      * Creates a signer with the specified identity name and scope.
  64.      * 
  65.      * @param name the identity name.   
  66.      *
  67.      * @param scope the scope of the identity. 
  68.      * 
  69.      * @exception KeyManagementException if there is already an identity 
  70.      * with the same name in the scope.
  71.      */
  72.     public Signer(String name, IdentityScope scope)
  73.     throws KeyManagementException {
  74.     super(name, scope);
  75.     }
  76.  
  77.     /**
  78.      * Returns this signer's private key.
  79.      * 
  80.      * @return this signer's private key, or null if the private key has
  81.      * not yet been set.
  82.      */
  83.     public PrivateKey getPrivateKey() {
  84.     check("get.private.key");
  85.     return privateKey;
  86.     }
  87.  
  88.    /**
  89.      * Sets the key pair (public key and private key) for this signer.
  90.      *
  91.      * @param pair an initialized key pair.
  92.      *
  93.      * @exception InvalidParameterException if the key pair is not
  94.      * properly initialized.
  95.      * @exception KeyException if the key pair cannot be set for any
  96.      * other reason.
  97.      */
  98.     public final void setKeyPair(KeyPair pair) 
  99.     throws InvalidParameterException, KeyException {
  100.     check("set.private.keypair");
  101.     PublicKey pub = pair.getPublic();
  102.     PrivateKey priv = pair.getPrivate();
  103.  
  104.     if (pub == null || priv == null) {
  105.         throw new InvalidParameterException();
  106.     }
  107.     setPublicKey(pub);
  108.     privateKey = priv;
  109.     }
  110.  
  111.     String printKeys() {
  112.     String keys = "";
  113.     PublicKey publicKey = getPublicKey();
  114.     if (publicKey != null && privateKey != null) {
  115.         keys = "\tpublic and private keys initialized";
  116.  
  117.     } else {
  118.         keys = "\tno keys";
  119.     }
  120.     return keys;
  121.     }
  122.  
  123.     /**
  124.      * Returns a string of information about the signer.
  125.      *
  126.      * @return a string of information about the signer.
  127.      */
  128.     public String toString() {
  129.     return "[Signer]" + super.toString();
  130.     }
  131. }
  132.  
  133.