Home | History | Annotate | Download | only in dsa
      1 package org.bouncycastle.jcajce.provider.asymmetric.dsa;
      2 
      3 import java.security.InvalidKeyException;
      4 import java.security.PrivateKey;
      5 import java.security.PublicKey;
      6 import java.security.interfaces.DSAParams;
      7 import java.security.interfaces.DSAPrivateKey;
      8 import java.security.interfaces.DSAPublicKey;
      9 import java.security.spec.DSAParameterSpec;
     10 
     11 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
     12 import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
     13 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
     14 import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
     15 import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
     16 import org.bouncycastle.crypto.params.DSAParameters;
     17 import org.bouncycastle.crypto.params.DSAPrivateKeyParameters;
     18 import org.bouncycastle.crypto.params.DSAPublicKeyParameters;
     19 
     20 /**
     21  * utility class for converting jce/jca DSA objects
     22  * objects into their org.bouncycastle.crypto counterparts.
     23  */
     24 public class DSAUtil
     25 {
     26     public static final ASN1ObjectIdentifier[] dsaOids =
     27     {
     28         X9ObjectIdentifiers.id_dsa,
     29         // Android-added: Add missing OID for DSA-with-SHA1
     30         X9ObjectIdentifiers.id_dsa_with_sha1,
     31         OIWObjectIdentifiers.dsaWithSHA1
     32     };
     33 
     34     public static boolean isDsaOid(
     35         ASN1ObjectIdentifier algOid)
     36     {
     37         for (int i = 0; i != dsaOids.length; i++)
     38         {
     39             if (algOid.equals(dsaOids[i]))
     40             {
     41                 return true;
     42             }
     43         }
     44 
     45         return false;
     46     }
     47 
     48     static DSAParameters toDSAParameters(DSAParams spec)
     49     {
     50         if (spec != null)
     51         {
     52              return new DSAParameters(spec.getP(), spec.getQ(), spec.getG());
     53         }
     54 
     55         return null;
     56     }
     57 
     58     static public AsymmetricKeyParameter generatePublicKeyParameter(
     59         PublicKey    key)
     60         throws InvalidKeyException
     61     {
     62         if (key instanceof BCDSAPublicKey)
     63         {
     64             return ((BCDSAPublicKey)key).engineGetKeyParameters();
     65         }
     66 
     67         if (key instanceof DSAPublicKey)
     68         {
     69             return new BCDSAPublicKey((DSAPublicKey)key).engineGetKeyParameters();
     70         }
     71 
     72         try
     73         {
     74             byte[]  bytes = key.getEncoded();
     75 
     76             BCDSAPublicKey bckey = new BCDSAPublicKey(SubjectPublicKeyInfo.getInstance(bytes));
     77 
     78             return bckey.engineGetKeyParameters();
     79         }
     80         catch (Exception e)
     81         {
     82             throw new InvalidKeyException("can't identify DSA public key: " + key.getClass().getName());
     83         }
     84     }
     85 
     86     static public AsymmetricKeyParameter generatePrivateKeyParameter(
     87         PrivateKey    key)
     88         throws InvalidKeyException
     89     {
     90         if (key instanceof DSAPrivateKey)
     91         {
     92             DSAPrivateKey    k = (DSAPrivateKey)key;
     93 
     94             return new DSAPrivateKeyParameters(k.getX(),
     95                 new DSAParameters(k.getParams().getP(), k.getParams().getQ(), k.getParams().getG()));
     96         }
     97 
     98         throw new InvalidKeyException("can't identify DSA private key.");
     99     }
    100 }
    101