Source for gnu.java.security.sig.rsa.RSASignatureFactory

   1: /* RSASignatureFactory.java -- A Factory class to instantiate RSA Signatures
   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.sig.rsa;
  40: 
  41: import java.util.Collections;
  42: import java.util.HashSet;
  43: import java.util.Iterator;
  44: import java.util.Set;
  45: 
  46: import gnu.java.security.Registry;
  47: import gnu.java.security.hash.HashFactory;
  48: import gnu.java.security.hash.IMessageDigest;
  49: import gnu.java.security.sig.ISignature;
  50: 
  51: /**
  52:  * A Factory class to instantiate RSA Signature classes.
  53:  */
  54: public class RSASignatureFactory
  55: {
  56:   private static Set names;
  57: 
  58:   /**
  59:    * Private constructor to enforce usage through Factory (class) methods.
  60:    */
  61:   private RSASignatureFactory()
  62:   {
  63:     super();
  64:   }
  65: 
  66:   /**
  67:    * Returns a new instance of an RSA Signature given its name. The name of an
  68:    * RSA Signature always starts with <code>rsa-</code>, followed by either
  69:    * <code>pss</code> or <code>pkcs1_v1.5</code>. An optional message digest
  70:    * name, to be used with the RSA signature may be specified by appending the
  71:    * hyphen chanaracter <code>-</code> followed by the canonical message digest
  72:    * algorithm name. When no message digest algorithm name is given, SHA-160 is
  73:    * used.
  74:    *
  75:    * @param name the composite RSA signature name.
  76:    * @return a new instance of an RSA Signature algorithm implementation.
  77:    * Returns <code>null</code> if the given name does not correspond to any
  78:    * supported RSA Signature encoding and message digest combination.
  79:    */
  80:   public static final ISignature getInstance(String name)
  81:   {
  82:     if (name == null)
  83:       return null;
  84: 
  85:     name = name.trim();
  86:     if (name.length() == 0)
  87:       return null;
  88: 
  89:     name = name.toLowerCase();
  90:     if (! name.startsWith(Registry.RSA_SIG_PREFIX))
  91:       return null;
  92: 
  93:     name = name.substring(Registry.RSA_SIG_PREFIX.length()).trim();
  94:     if (name.startsWith(Registry.RSA_PSS_ENCODING))
  95:       return getPSSSignature(name);
  96:     else if (name.startsWith(Registry.RSA_PKCS1_V1_5_ENCODING))
  97:       return getPKCS1Signature(name);
  98:     else
  99:       return null;
 100:   }
 101: 
 102:   /**
 103:    * Returns a {@link Set} of names of <i>RSA</i> signatures supported by this
 104:    * <i>Factory</i>.
 105:    *
 106:    * @return a {@link Set} of RSA Signature algorithm names (Strings).
 107:    */
 108:   public static synchronized final Set getNames()
 109:   {
 110:     if (names == null)
 111:       {
 112:         Set hashNames = HashFactory.getNames();
 113:         HashSet hs = new HashSet();
 114:         for (Iterator it = hashNames.iterator(); it.hasNext();)
 115:           {
 116:             String mdName = (String) it.next();
 117:             hs.add(Registry.RSA_PSS_SIG + "-" + mdName);
 118:           }
 119: 
 120:         hs.add(Registry.RSA_PKCS1_V1_5_SIG + "-" + Registry.MD2_HASH);
 121:         hs.add(Registry.RSA_PKCS1_V1_5_SIG + "-" + Registry.MD5_HASH);
 122:         hs.add(Registry.RSA_PKCS1_V1_5_SIG + "-" + Registry.SHA160_HASH);
 123:         hs.add(Registry.RSA_PKCS1_V1_5_SIG + "-" + Registry.SHA256_HASH);
 124:         hs.add(Registry.RSA_PKCS1_V1_5_SIG + "-" + Registry.SHA384_HASH);
 125:         hs.add(Registry.RSA_PKCS1_V1_5_SIG + "-" + Registry.SHA512_HASH);
 126: 
 127:         names = Collections.unmodifiableSet(hs);
 128:       }
 129: 
 130:     return names;
 131:   }
 132: 
 133:   private static final ISignature getPSSSignature(String name)
 134:   {
 135:     name = name.substring(Registry.RSA_PSS_ENCODING.length()).trim();
 136:     // remove the hyphen if found at the beginning
 137:     if (name.startsWith("-"))
 138:       name = name.substring(1).trim();
 139: 
 140:     IMessageDigest md;
 141:     if (name.length() == 0)
 142:       md = HashFactory.getInstance(Registry.SHA160_HASH);
 143:     else
 144:       {
 145:         // check if there is such a hash
 146:         md = HashFactory.getInstance(name);
 147:         if (md == null)
 148:           return null;
 149:       }
 150: 
 151:     ISignature result = new RSAPSSSignature(md, 0);
 152:     return result;
 153:   }
 154: 
 155:   private static final ISignature getPKCS1Signature(String name)
 156:   {
 157:     name = name.substring(Registry.RSA_PKCS1_V1_5_ENCODING.length()).trim();
 158:     // remove the hyphen if found at the beginning
 159:     if (name.startsWith("-"))
 160:       name = name.substring(1).trim();
 161: 
 162:     IMessageDigest md;
 163:     if (name.length() == 0)
 164:       md = HashFactory.getInstance(Registry.SHA160_HASH);
 165:     else
 166:       {
 167:         // check if there is such a hash
 168:         md = HashFactory.getInstance(name);
 169:         if (md == null)
 170:           return null;
 171:       }
 172: 
 173:     ISignature result = new RSAPKCS1V1_5Signature(md);
 174:     return result;
 175:   }
 176: }