Source for gnu.java.security.jce.sig.RSAKeyFactory

   1: /* RSAKeyFactory.java -- RSA key-factory JCE Adapter
   2:    Copyright (C) 2006 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.jce.sig;
  40: 
  41: import gnu.java.security.Registry;
  42: import gnu.java.security.key.rsa.GnuRSAPrivateKey;
  43: import gnu.java.security.key.rsa.GnuRSAPublicKey;
  44: import gnu.java.security.key.rsa.RSAKeyPairPKCS8Codec;
  45: import gnu.java.security.key.rsa.RSAKeyPairX509Codec;
  46: 
  47: import java.math.BigInteger;
  48: import java.security.InvalidKeyException;
  49: import java.security.Key;
  50: import java.security.KeyFactorySpi;
  51: import java.security.PrivateKey;
  52: import java.security.PublicKey;
  53: import java.security.interfaces.RSAPrivateCrtKey;
  54: import java.security.interfaces.RSAPrivateKey;
  55: import java.security.interfaces.RSAPublicKey;
  56: import java.security.spec.InvalidKeySpecException;
  57: import java.security.spec.KeySpec;
  58: import java.security.spec.PKCS8EncodedKeySpec;
  59: import java.security.spec.RSAPrivateCrtKeySpec;
  60: import java.security.spec.RSAPrivateKeySpec;
  61: import java.security.spec.RSAPublicKeySpec;
  62: import java.security.spec.X509EncodedKeySpec;
  63: 
  64: public class RSAKeyFactory
  65:     extends KeyFactorySpi
  66: {
  67:   // implicit 0-arguments constructor
  68: 
  69:   protected PublicKey engineGeneratePublic(KeySpec keySpec)
  70:       throws InvalidKeySpecException
  71:   {
  72:     if (keySpec instanceof RSAPublicKeySpec)
  73:       {
  74:         RSAPublicKeySpec spec = (RSAPublicKeySpec) keySpec;
  75:         BigInteger n = spec.getModulus();
  76:         BigInteger e = spec.getPublicExponent();
  77:         return new GnuRSAPublicKey(Registry.X509_ENCODING_ID, n, e);
  78:       }
  79:     if (keySpec instanceof X509EncodedKeySpec)
  80:       {
  81:         X509EncodedKeySpec spec = (X509EncodedKeySpec) keySpec;
  82:         byte[] encoded = spec.getEncoded();
  83:         PublicKey result;
  84:         try
  85:           {
  86:             return new RSAKeyPairX509Codec().decodePublicKey(encoded);
  87:           }
  88:         catch (RuntimeException x)
  89:           {
  90:             throw new InvalidKeySpecException(x.getMessage(), x);
  91:           }
  92:       }
  93:     throw new InvalidKeySpecException("Unsupported (public) key specification");
  94:   }
  95: 
  96:   protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
  97:       throws InvalidKeySpecException
  98:   {
  99:     if (keySpec instanceof RSAPrivateCrtKeySpec)
 100:       {
 101:         RSAPrivateCrtKeySpec spec = (RSAPrivateCrtKeySpec) keySpec;
 102:         BigInteger n = spec.getModulus();
 103:         BigInteger e = spec.getPublicExponent();
 104:         BigInteger d = spec.getPrivateExponent();
 105:         BigInteger p = spec.getPrimeP();
 106:         BigInteger q = spec.getPrimeQ();
 107:         BigInteger dP = spec.getPrimeExponentP();
 108:         BigInteger dQ = spec.getPrimeExponentQ();
 109:         BigInteger qInv = spec.getCrtCoefficient();
 110:         return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID,
 111:                                     n, e, d, p, q, dP, dQ, qInv);
 112:       }
 113:     if (keySpec instanceof PKCS8EncodedKeySpec)
 114:       {
 115:         PKCS8EncodedKeySpec spec = (PKCS8EncodedKeySpec) keySpec;
 116:         byte[] encoded = spec.getEncoded();
 117:         PrivateKey result;
 118:         try
 119:           {
 120:             return new RSAKeyPairPKCS8Codec().decodePrivateKey(encoded);
 121:           }
 122:         catch (RuntimeException x)
 123:           {
 124:             throw new InvalidKeySpecException(x.getMessage(), x);
 125:           }
 126:       }
 127:     throw new InvalidKeySpecException("Unsupported (private) key specification");
 128:   }
 129: 
 130:   protected KeySpec engineGetKeySpec(Key key, Class keySpec)
 131:       throws InvalidKeySpecException
 132:   {
 133:     if (key instanceof RSAPublicKey)
 134:       {
 135:         if (keySpec.isAssignableFrom(RSAPublicKeySpec.class))
 136:         {
 137:           RSAPublicKey rsaKey = (RSAPublicKey) key;
 138:           BigInteger n = rsaKey.getModulus();
 139:           BigInteger e = rsaKey.getPublicExponent();
 140:           return new RSAPublicKeySpec(n, e);
 141:         }
 142:         if (keySpec.isAssignableFrom(X509EncodedKeySpec.class))
 143:           {
 144:             if (key instanceof GnuRSAPublicKey)
 145:               {
 146:                 GnuRSAPublicKey rsaKey = (GnuRSAPublicKey) key;
 147:                 byte[] encoded = rsaKey.getEncoded(Registry.X509_ENCODING_ID);
 148:                 return new X509EncodedKeySpec(encoded);
 149:               }
 150: 
 151:             if (Registry.X509_ENCODING_SORT_NAME.equalsIgnoreCase(key.getFormat()))
 152:               {
 153:                 byte[] encoded = key.getEncoded();
 154:                 return new X509EncodedKeySpec(encoded);
 155:               }
 156:             throw new InvalidKeySpecException(
 157:                 "Wrong key type or unsupported (public) key specification");
 158:           }
 159:         throw new InvalidKeySpecException("Unsupported (public) key specification");
 160:       }
 161:     if ((key instanceof RSAPrivateCrtKey)
 162:         && keySpec.isAssignableFrom(RSAPrivateCrtKeySpec.class))
 163:       {
 164:         RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
 165:         BigInteger n = rsaKey.getModulus();
 166:         BigInteger e = rsaKey.getPublicExponent();
 167:         BigInteger d = rsaKey.getPrivateExponent();
 168:         BigInteger p = rsaKey.getPrimeP();
 169:         BigInteger q = rsaKey.getPrimeQ();
 170:         BigInteger dP = rsaKey.getPrimeExponentP();
 171:         BigInteger dQ = rsaKey.getPrimeExponentQ();
 172:         BigInteger qInv = rsaKey.getCrtCoefficient();
 173:         return new RSAPrivateCrtKeySpec(n, e, d, p, q, dP, dQ, qInv);
 174:       }
 175:     if ((key instanceof RSAPrivateKey)
 176:         && keySpec.isAssignableFrom(RSAPrivateKeySpec.class))
 177:       {
 178:         RSAPrivateKey rsaKey = (RSAPrivateKey) key;
 179:         BigInteger n = rsaKey.getModulus();
 180:         BigInteger d = rsaKey.getPrivateExponent();
 181:         return new RSAPrivateKeySpec(n, d);
 182:       }
 183:     if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class))
 184:       {
 185:         if (key instanceof GnuRSAPrivateKey)
 186:           {
 187:             GnuRSAPrivateKey rsaKey = (GnuRSAPrivateKey) key;
 188:             byte[] encoded = rsaKey.getEncoded(Registry.PKCS8_ENCODING_ID);
 189:             return new PKCS8EncodedKeySpec(encoded);
 190:           }
 191:         if (Registry.PKCS8_ENCODING_SHORT_NAME.equalsIgnoreCase(key.getFormat()))
 192:           {
 193:             byte[] encoded = key.getEncoded();
 194:             return new PKCS8EncodedKeySpec(encoded);
 195:           }
 196:         throw new InvalidKeySpecException(
 197:             "Wrong key type or unsupported (private) key specification");
 198:       }
 199:     throw new InvalidKeySpecException(
 200:         "Wrong key type or unsupported key specification");
 201:   }
 202: 
 203:   protected Key engineTranslateKey(Key key) throws InvalidKeyException
 204:   {
 205:     if ((key instanceof GnuRSAPublicKey) || (key instanceof GnuRSAPrivateKey))
 206:       return key;
 207: 
 208:     if (key instanceof RSAPublicKey)
 209:       {
 210:         RSAPublicKey rsaKey = (RSAPublicKey) key;
 211:         BigInteger n = rsaKey.getModulus();
 212:         BigInteger e = rsaKey.getPublicExponent();
 213:         return new GnuRSAPublicKey(Registry.X509_ENCODING_ID, n, e);
 214:       }
 215:     if (key instanceof RSAPrivateCrtKey)
 216:       {
 217:         RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
 218:         BigInteger n = rsaKey.getModulus();
 219:         BigInteger e = rsaKey.getPublicExponent();
 220:         BigInteger d = rsaKey.getPrivateExponent();
 221:         BigInteger p = rsaKey.getPrimeP();
 222:         BigInteger q = rsaKey.getPrimeQ();
 223:         BigInteger dP = rsaKey.getPrimeExponentP();
 224:         BigInteger dQ = rsaKey.getPrimeExponentQ();
 225:         BigInteger qInv = rsaKey.getCrtCoefficient();
 226:         return new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID,
 227:                                     n, e, d, p, q, dP, dQ, qInv);
 228:       }
 229:     throw new InvalidKeyException("Unsupported key type");
 230:   }
 231: }