Source for gnu.javax.crypto.keyring.PrivateKeyEntry

   1: /* PrivateKeyEntry.java --
   2:    Copyright (C) 2003, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2 of the License, or (at
   9: your option) any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: USA
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version.  */
  37: 
  38: 
  39: package gnu.javax.crypto.keyring;
  40: 
  41: import gnu.java.security.key.IKeyPairCodec;
  42: import gnu.java.security.key.KeyPairCodecFactory;
  43: import gnu.java.security.key.dss.DSSPrivateKey;
  44: import gnu.java.security.key.rsa.GnuRSAPrivateKey;
  45: import gnu.javax.crypto.key.GnuSecretKey;
  46: import gnu.javax.crypto.key.dh.GnuDHPrivateKey;
  47: 
  48: import java.io.DataInputStream;
  49: import java.io.IOException;
  50: import java.security.Key;
  51: import java.security.KeyFactory;
  52: import java.security.PrivateKey;
  53: import java.security.spec.PKCS8EncodedKeySpec;
  54: import java.util.Date;
  55: 
  56: /**
  57:  * An immutable class representing a private or secret key entry.
  58:  */
  59: public final class PrivateKeyEntry
  60:     extends PrimitiveEntry
  61: {
  62:   public static final int TYPE = 7;
  63:   /** The key. */
  64:   private Key key;
  65: 
  66:   /**
  67:    * Creates a new key entry.
  68:    *
  69:    * @param key The key.
  70:    * @param creationDate The entry creation date.
  71:    * @param properties The entry properties.
  72:    * @throws IllegalArgumentException If any parameter is null.
  73:    */
  74:   public PrivateKeyEntry(Key key, Date creationDate, Properties properties)
  75:   {
  76:     super(TYPE, creationDate, properties);
  77:     if (key == null)
  78:       throw new IllegalArgumentException("no private key");
  79:     if (! (key instanceof PrivateKey) && ! (key instanceof GnuSecretKey))
  80:       throw new IllegalArgumentException("not a private or secret key");
  81:     this.key = key;
  82:   }
  83: 
  84:   private PrivateKeyEntry()
  85:   {
  86:     super(TYPE);
  87:   }
  88: 
  89:   public static PrivateKeyEntry decode(DataInputStream in) throws IOException
  90:   {
  91:     PrivateKeyEntry entry = new PrivateKeyEntry();
  92:     entry.defaultDecode(in);
  93:     String type = entry.properties.get("type");
  94:     if (type == null)
  95:       throw new MalformedKeyringException("no key type");
  96:     if (type.equalsIgnoreCase("RAW-DSS"))
  97:       {
  98:         IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dss");
  99:         entry.key = coder.decodePrivateKey(entry.payload);
 100:       }
 101:     else if (type.equalsIgnoreCase("RAW-RSA"))
 102:       {
 103:         IKeyPairCodec coder = KeyPairCodecFactory.getInstance("rsa");
 104:         entry.key = coder.decodePrivateKey(entry.payload);
 105:       }
 106:     else if (type.equalsIgnoreCase("RAW-DH"))
 107:       {
 108:         IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dh");
 109:         entry.key = coder.decodePrivateKey(entry.payload);
 110:       }
 111:     else if (type.equalsIgnoreCase("RAW"))
 112:       entry.key = new GnuSecretKey(entry.payload, null);
 113:     else if (type.equalsIgnoreCase("PKCS8"))
 114:       {
 115:         try
 116:           {
 117:             KeyFactory kf = KeyFactory.getInstance("RSA");
 118:             PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(entry.payload);
 119:             entry.key = kf.generatePrivate(ks);
 120:           }
 121:         catch (Exception ignored)
 122:           {
 123:           }
 124:         if (entry.key == null)
 125:           {
 126:             try
 127:               {
 128:                 KeyFactory kf = KeyFactory.getInstance("DSA");
 129:                 PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(entry.payload);
 130:                 entry.key = kf.generatePrivate(ks);
 131:               }
 132:             catch (Exception ignored)
 133:               {
 134:               }
 135:             if (entry.key == null)
 136:               throw new MalformedKeyringException("could not decode PKCS#8 key");
 137:           }
 138:       }
 139:     else
 140:       throw new MalformedKeyringException("unsupported key type " + type);
 141:     return entry;
 142:   }
 143: 
 144:   /**
 145:    * Returns this entry's key.
 146:    *
 147:    * @return The key.
 148:    */
 149:   public Key getKey()
 150:   {
 151:     return key;
 152:   }
 153: 
 154:   protected void encodePayload() throws IOException
 155:   {
 156:     String format = key.getFormat();
 157:     if (key instanceof DSSPrivateKey)
 158:       {
 159:         properties.put("type", "RAW-DSS");
 160:         IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dss");
 161:         payload = coder.encodePrivateKey((PrivateKey) key);
 162:       }
 163:     else if (key instanceof GnuRSAPrivateKey)
 164:       {
 165:         properties.put("type", "RAW-RSA");
 166:         IKeyPairCodec coder = KeyPairCodecFactory.getInstance("rsa");
 167:         payload = coder.encodePrivateKey((PrivateKey) key);
 168:       }
 169:     else if (key instanceof GnuDHPrivateKey)
 170:       {
 171:         properties.put("type", "RAW-DH");
 172:         IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dh");
 173:         payload = coder.encodePrivateKey((PrivateKey) key);
 174:       }
 175:     else if (key instanceof GnuSecretKey)
 176:       {
 177:         properties.put("type", "RAW");
 178:         payload = key.getEncoded();
 179:       }
 180:     else if (format != null && format.equals("PKCS#8"))
 181:       {
 182:         properties.put("type", "PKCS8");
 183:         payload = key.getEncoded();
 184:       }
 185:     else
 186:       throw new IllegalArgumentException("unsupported private key");
 187:   }
 188: 
 189:   public String toString()
 190:   {
 191:     return "PrivateKeyEntry{key="
 192:            + (key == null ? "-" : key.getClass().getName()) + "}";
 193:   }
 194: }