Source for gnu.javax.crypto.keyring.PublicKeyEntry

   1: /* PublicKeyEntry.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.DSSPublicKey;
  44: import gnu.java.security.key.rsa.GnuRSAPublicKey;
  45: import gnu.javax.crypto.key.dh.GnuDHPublicKey;
  46: 
  47: import java.io.DataInputStream;
  48: import java.io.IOException;
  49: import java.security.KeyFactory;
  50: import java.security.PublicKey;
  51: import java.security.spec.X509EncodedKeySpec;
  52: import java.util.Date;
  53: 
  54: public final class PublicKeyEntry
  55:     extends PrimitiveEntry
  56: {
  57:   public static final int TYPE = 6;
  58:   private PublicKey key;
  59: 
  60:   public PublicKeyEntry(PublicKey key, Date creationDate, Properties properties)
  61:   {
  62:     super(TYPE, creationDate, properties);
  63:     if (key == null)
  64:       throw new IllegalArgumentException("no key specified");
  65:     this.key = key;
  66:   }
  67: 
  68:   private PublicKeyEntry()
  69:   {
  70:     super(TYPE);
  71:   }
  72: 
  73:   public static PublicKeyEntry decode(DataInputStream in) throws IOException
  74:   {
  75:     PublicKeyEntry entry = new PublicKeyEntry();
  76:     entry.defaultDecode(in);
  77:     String type = entry.properties.get("type");
  78:     if (type == null)
  79:       throw new MalformedKeyringException("no key type");
  80:     if (type.equalsIgnoreCase("RAW-DSS"))
  81:       {
  82:         IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dss");
  83:         entry.key = coder.decodePublicKey(entry.payload);
  84:       }
  85:     else if (type.equalsIgnoreCase("RAW-RSA"))
  86:       {
  87:         IKeyPairCodec coder = KeyPairCodecFactory.getInstance("rsa");
  88:         entry.key = coder.decodePublicKey(entry.payload);
  89:       }
  90:     else if (type.equalsIgnoreCase("RAW-DH"))
  91:       {
  92:         IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dh");
  93:         entry.key = coder.decodePublicKey(entry.payload);
  94:       }
  95:     else if (type.equalsIgnoreCase("X.509"))
  96:       {
  97:         try
  98:           {
  99:             KeyFactory kf = KeyFactory.getInstance("RSA");
 100:             entry.key = kf.generatePublic(new X509EncodedKeySpec(entry.payload));
 101:           }
 102:         catch (Exception x)
 103:           {
 104:           }
 105:         if (entry.key == null)
 106:           {
 107:             try
 108:               {
 109:                 KeyFactory kf = KeyFactory.getInstance("DSA");
 110:                 entry.key = kf.generatePublic(new X509EncodedKeySpec(entry.payload));
 111:               }
 112:             catch (Exception x)
 113:               {
 114:               }
 115:             if (entry.key == null)
 116:               throw new MalformedKeyringException("could not decode X.509 key");
 117:           }
 118:       }
 119:     else
 120:       throw new MalformedKeyringException("unsupported public key type: " + type);
 121:     return entry;
 122:   }
 123: 
 124:   /**
 125:    * Returns the public key.
 126:    *
 127:    * @return The public key.
 128:    */
 129:   public PublicKey getKey()
 130:   {
 131:     return key;
 132:   }
 133: 
 134:   protected void encodePayload() throws IOException
 135:   {
 136:     if (key instanceof DSSPublicKey)
 137:       {
 138:         properties.put("type", "RAW-DSS");
 139:         IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dss");
 140:         payload = coder.encodePublicKey(key);
 141:       }
 142:     else if (key instanceof GnuRSAPublicKey)
 143:       {
 144:         properties.put("type", "RAW-RSA");
 145:         IKeyPairCodec coder = KeyPairCodecFactory.getInstance("rsa");
 146:         payload = coder.encodePublicKey(key);
 147:       }
 148:     else if (key instanceof GnuDHPublicKey)
 149:       {
 150:         properties.put("type", "RAW-DH");
 151:         IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dh");
 152:         payload = coder.encodePublicKey(key);
 153:       }
 154:     else if (key.getFormat() != null && key.getFormat().equals("X.509"))
 155:       {
 156:         properties.put("type", "X.509");
 157:         payload = key.getEncoded();
 158:       }
 159:     else
 160:       throw new IllegalArgumentException("cannot encode public key");
 161:   }
 162: }