例: 自動鍵生成用の鍵または鍵ペア生成クラスの作成

暗号オペレーションの鍵を生成するクラスは、自動的に作成できます。 この機能を使用すると、鍵管理インフラストラクチャーは定義済みの鍵セットの鍵のリストを維持することができ、アプリケーションはこれらの鍵にアクセスできます。

事前に定義した頻度で新規の鍵生成をスケジュールできます。 鍵生成の頻度は、ご使用のデータのセキュリティーに影響を与えることに注意してください。 例えば、パーシスタント・データの場合、古い鍵の有効期限が切れるために頻繁に鍵が生成されるリアルタイム通信よりは、少ない頻度で鍵生成をスケジュールすることがあります。

鍵生成クラスを作成する場合は、共有鍵または鍵ペアのどちらを作成するかを決定します。これにより、使用するインターフェースが決まります。

共有鍵を作成する場合は、以下の例を参照してください。ここでは、KeyGenerator クラスを使用して com.ibm.websphere.crypto.KeyGenerator インターフェースを実装します。 このインターフェースは、JCEKS 鍵ストア・タイプ内に SecretKey として保管されている java.security.Key 鍵を戻します。 秘密鍵の保管をサポートする他の鍵ストア・タイプを使用することもできます。

package com.ibm.test;

import java.util.*;
import com.ibm.ws.ssl.core.*;
import com.ibm.ws.ssl.config.*;
import com.ibm.websphere.crypto.KeyException;

public class KeyGenerator implements com.ibm.websphere.crypto.KeyGenerator
{
    private java.util.Properties customProperties = null;
    private java.security.Key secretKey = null;

    public KeyGenerator()
    {
    }

     /**
       * This method is called to pass any custom properties configured with
       * the KeySet to the implementation of this interface.
      *
      * @param java.util.Properties
      **/
    public void init (java.util.Properties customProps)
    {
        customProperties = customProps;
    }

     /**
       * This method is called whenever a key needs to be generated either
       * from the schedule or manually requested. The key is stored in the
       * KeyStore referenced by the configured KeySet that contains the
       * keyGenerationClass implementing this interface. The implementation of
       * this interface manages the key type. The user of the KeySet
       * must know the type that is returned by this keyGenerationClass.
      *
      * @return java.security.Key
      * @throws com.ibm.websphere.crypto.KeyException
      **/
    public java.security.Key generateKey () throws KeyException
    {
        try
        {
             // Assume generate3DESKey is there to create the key.
            byte[] tripleDESKey = generate3DESKey();
            secretKey = new javax.crypto.spec.SecretKeySpec(tripleDESKey, 0, 24, "3DES");
            
            if (secretKey != null)
            {
                return secretKey;
            }
            else
            {
                throw new com.ibm.websphere.crypto.KeyException ("Key could not be generated.");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();  // handle exception
        }
    }
}

鍵ペアを作成する場合は、以下の例を参照してください。ここでは、KeyPairGenerator クラスを使用して com.ibm.websphere.crypto.KeyPairGenerator インターフェースを実装します。

package com.ibm.test;

import java.util.*;
import javax.crypto.spec.SecretKeySpec;
import com.ibm.websphere.crypto.KeyException;

/**
  * This implementation defines the method to generate a java.security.KeyPair.
  * When a keyGeneration class implements this method, the generateKeyPair method
  * is called and a KeyPair is stored in the keystore. The isKeyPair
  * attribute is ignored since the KeyGenerationClass is an
  * implementation of KeyPairGenerator. The isKeyPair attributes is for when
  * the keys already exist in a KeyStore, and are just read (not
  * generating them).
 * 
 * @author IBM Corporation
 * @version WebSphere Application Server 6.1
 * @since WebSphere Application Server 6.1
 **/
public class KeyPairGenerator implements com.ibm.websphere.crypto.KeyPairGenerator
{
    private java.util.Properties customProperties = null;

    public KeyPairGenerator()
    {
    }

     /**
       * This method is called to pass any custom properties configured with
       * the KeySet to the implementation of this interface.
     *
      * @param java.util.Properties
      **/
    public void init (java.util.Properties customProps)
    {
        customProperties = customProps;
    }

     /**
      * This method is called whenever a key needs to be generated either
       * from the schedule or manually requested and isKeyPair=true in the KeySet
       * configuration. The key is stored in the KeyStore referenced by
       * the configured KeySet which contains the keyGenerationClass implementing
       * this interface. The implementation of this interface manages the
       * type of the key. The user of the KeySet must know the type that
       * is returned by this keyGenerationClass.
      *
      * @return com.ibm.websphere.crypto.KeyPair
      * @throws com.ibm.websphere.crypto.KeyException
      **/
    public com.ibm.websphere.crypto.KeyPair generateKeyPair () throws KeyException
    {
        try
        {
            java.security.KeyPair keyPair = generateKeyPair();
            
             // Store as SecretKeySpec
            if (keyPair != null)
            {
                java.security.PrivateKey privKey = keyPair.getPrivate();
                java.security.PublicKey pubKey = keyPair.getPublic();

                SecretKeySpec publicKeyAsSecretKey = new SecretKeySpec (pubKey.getEncoded(), "RSA_PUBLIC");
                SecretKeySpec privateKeyAsSecretKey = new SecretKeySpec (privKey.getEncoded(), "RSA_PRIVATE");

                com.ibm.websphere.crypto.KeyPair pair = new 	
								com.ibm.websphere.crypto.KeyPair(publicKeyAsSecretKey, privateKeyAsSecretKey);
                return pair;
            }
            else
            {
                throw new com.ibm.websphere.crypto.KeyException ("Key pair could not be generated.");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();  // handle exception
        }
    }
}

このインターフェースは、X509Certificate および PrivateKey オブジェクト、 または PublicKey および PrivateKey オブジェクトのいずれかを含むことができる com.ibm.websphere.crypto.KeyPair 鍵ペアを戻します。 com.ibm.websphere.crypto.KeyPair インターフェースに X509Certificate および PrivateKey オブジェクトが含まれる場合、証明書および秘密鍵は鍵ストアに保管されています。 結果として、すべての KeyStore タイプを使用できます。

com.ibm.websphere.crypto.KeyPair インターフェースに PublicKey および PrivateKey オブジェクトが含まれる場合、エンコードされた値を保管するには、それらを SecretKeySpec オブジェクトに変換する必要があります。 WebSphere® Application Server ランタイムは、 鍵ペアを秘密鍵として保管および取得します。 サーバーがハンドシェーク中にペアを取得した場合、ランタイムは鍵ペアを PublicKey および PrivateKey オブジェクトに変換し直します。

以下のコンストラクターを使用して、com.ibm.websphere.crypto.KeyPair インターフェースを作成します。
  • 公開および秘密コンストラクター
    public KeyPair(java.security.Key publicKey, java.security.Key privateKey)
  • 証明書および秘密コンストラクター
    public KeyPair(java.security.cert.Certificate[] certChain, 
    java.security.Key privateKey)

上記のコード・サンプルでは、公開および秘密コンストラクターを使用する KeyPairGenerator クラスを示しています。 このクラスへのそれぞれの呼び出しごとに、新しい固有の鍵ペアが生成され、isKeyPair=true の場合に、このクラスが KeySet により呼び出されて新しい鍵ペアを作成します。 鍵セットのバージョン番号は、 呼び出されるたびにインクリメントされます。


トピックのタイプを示すアイコン 参照トピック



タイム・スタンプ・アイコン 最終更新: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=rsec_ssldevkeypairgen
ファイル名:rsec_ssldevkeypairgen.html