Source for gnu.java.security.provider.RSAKeyFactory

   1: /* RSAKeyFactory.java -- RSA key factory.
   2:    Copyright (C) 2004  Free Software Foundation, Inc.
   3: 
   4: This file is 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, or (at your option)
   9: 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; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 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.java.security.provider;
  40: 
  41: import java.security.InvalidKeyException;
  42: import java.security.Key;
  43: import java.security.KeyFactorySpi;
  44: import java.security.PrivateKey;
  45: import java.security.PublicKey;
  46: 
  47: import java.security.interfaces.RSAPrivateCrtKey;
  48: import java.security.interfaces.RSAPrivateKey;
  49: import java.security.interfaces.RSAPublicKey;
  50: 
  51: import java.security.spec.InvalidKeySpecException;
  52: import java.security.spec.KeySpec;
  53: import java.security.spec.PKCS8EncodedKeySpec;
  54: import java.security.spec.RSAPrivateCrtKeySpec;
  55: import java.security.spec.RSAPrivateKeySpec;
  56: import java.security.spec.RSAPublicKeySpec;
  57: import java.security.spec.X509EncodedKeySpec;
  58: 
  59: public class RSAKeyFactory extends KeyFactorySpi
  60: {
  61: 
  62:   // Default constructor.
  63:   // -------------------------------------------------------------------------
  64: 
  65:   // Instance methods.
  66:   // -------------------------------------------------------------------------
  67: 
  68:   protected PrivateKey engineGeneratePrivate(KeySpec spec)
  69:     throws InvalidKeySpecException
  70:   {
  71:     if (spec instanceof RSAPrivateCrtKeySpec)
  72:       {
  73:         return new GnuRSAPrivateKey((RSAPrivateCrtKeySpec) spec);
  74:       }
  75:     if (spec instanceof RSAPrivateKeySpec)
  76:       {
  77:         return new GnuRSAPrivateKey(new RSAPrivateCrtKeySpec(
  78:           ((RSAPrivateKeySpec) spec).getModulus(), null,
  79:           ((RSAPrivateKeySpec) spec).getPrivateExponent(), null,
  80:           null, null, null, null));
  81:       }
  82:     if (spec instanceof PKCS8EncodedKeySpec)
  83:       {
  84:         EncodedKeyFactory ekf = new EncodedKeyFactory();
  85:         PrivateKey pk = ekf.engineGeneratePrivate(spec);
  86:         if (pk instanceof RSAPrivateKey)
  87:           return pk;
  88:       }
  89:     throw new InvalidKeySpecException();
  90:   }
  91: 
  92:   protected PublicKey engineGeneratePublic(KeySpec spec)
  93:     throws InvalidKeySpecException
  94:   {
  95:     if (spec instanceof RSAPublicKeySpec)
  96:       {
  97:         return new GnuRSAPublicKey((RSAPublicKeySpec) spec);
  98:       }
  99:     if (spec instanceof X509EncodedKeySpec)
 100:       {
 101:         EncodedKeyFactory ekf = new EncodedKeyFactory();
 102:         PublicKey pk = ekf.engineGeneratePublic(spec);
 103:         if (pk instanceof RSAPublicKey)
 104:           return pk;
 105:       }
 106:     throw new InvalidKeySpecException();
 107:   }
 108: 
 109:   protected KeySpec engineGetKeySpec(Key key, Class keySpec)
 110:     throws InvalidKeySpecException
 111:   {
 112:     if (keySpec.isAssignableFrom(RSAPrivateCrtKeySpec.class)
 113:         && (key instanceof RSAPrivateCrtKey))
 114:       {
 115:         return new RSAPrivateCrtKeySpec(
 116:           ((RSAPrivateCrtKey) key).getModulus(),
 117:           ((RSAPrivateCrtKey) key).getPublicExponent(),
 118:           ((RSAPrivateCrtKey) key).getPrivateExponent(),
 119:           ((RSAPrivateCrtKey) key).getPrimeP(),
 120:           ((RSAPrivateCrtKey) key).getPrimeQ(),
 121:           ((RSAPrivateCrtKey) key).getPrimeExponentP(),
 122:           ((RSAPrivateCrtKey) key).getPrimeExponentQ(),
 123:           ((RSAPrivateCrtKey) key).getCrtCoefficient());
 124:       }
 125:     if (keySpec.isAssignableFrom(RSAPrivateKeySpec.class)
 126:         && (key instanceof RSAPrivateKey))
 127:       {
 128:         return new RSAPrivateKeySpec(
 129:           ((RSAPrivateCrtKey) key).getModulus(),
 130:           ((RSAPrivateCrtKey) key).getPrivateExponent());
 131:       }
 132:     if (keySpec.isAssignableFrom(RSAPublicKeySpec.class)
 133:         && (key instanceof RSAPublicKey))
 134:       {
 135:         return new RSAPublicKeySpec(
 136:           ((RSAPrivateCrtKey) key).getModulus(),
 137:           ((RSAPrivateCrtKey) key).getPublicExponent());
 138:       }
 139:     if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class)
 140:         && key.getFormat().equalsIgnoreCase("PKCS#8"))
 141:       {
 142:         return new PKCS8EncodedKeySpec(key.getEncoded());
 143:       }
 144:     if (keySpec.isAssignableFrom(X509EncodedKeySpec.class)
 145:         && key.getFormat().equalsIgnoreCase("X.509"))
 146:       {
 147:         return new X509EncodedKeySpec(key.getEncoded());
 148:       }
 149:     throw new InvalidKeySpecException();
 150:   }
 151: 
 152:   protected Key engineTranslateKey(Key key) throws InvalidKeyException
 153:   {
 154:     if (key instanceof RSAPrivateCrtKey)
 155:       {
 156:         return new GnuRSAPrivateKey(new RSAPrivateCrtKeySpec(
 157:           ((RSAPrivateCrtKey) key).getModulus(),
 158:           ((RSAPrivateCrtKey) key).getPublicExponent(),
 159:           ((RSAPrivateCrtKey) key).getPrivateExponent(),
 160:           ((RSAPrivateCrtKey) key).getPrimeP(),
 161:           ((RSAPrivateCrtKey) key).getPrimeQ(),
 162:           ((RSAPrivateCrtKey) key).getPrimeExponentP(),
 163:           ((RSAPrivateCrtKey) key).getPrimeExponentQ(),
 164:           ((RSAPrivateCrtKey) key).getCrtCoefficient()));
 165:       }
 166:     if (key instanceof RSAPrivateKey)
 167:       {
 168:         return new GnuRSAPrivateKey(new RSAPrivateCrtKeySpec(
 169:           ((RSAPrivateKey) key).getModulus(), null,
 170:           ((RSAPrivateKey) key).getPrivateExponent(), null,
 171:           null, null, null, null));
 172:       }
 173:     if (key instanceof RSAPublicKey)
 174:       {
 175:         return new GnuRSAPublicKey(new RSAPublicKeySpec(
 176:           ((RSAPrivateCrtKey) key).getModulus(),
 177:           ((RSAPrivateCrtKey) key).getPublicExponent()));
 178:       }
 179:     throw new InvalidKeyException();
 180:   }
 181: }