Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

JCODHPublicKey.java

00001 package edu.virtualschool.jco.unfinished;
00002 
00003 import java.io.ByteArrayInputStream;
00004 import java.io.ByteArrayOutputStream;
00005 import java.io.IOException;
00006 import java.math.BigInteger;
00007 
00008 import org.bouncycastle.asn1.ASN1Sequence;
00009 import org.bouncycastle.asn1.DERInputStream;
00010 import org.bouncycastle.asn1.DERInteger;
00011 import org.bouncycastle.asn1.DEROutputStream;
00012 import org.bouncycastle.asn1.pkcs.DHParameter;
00013 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
00014 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
00015 import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
00016 import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
00017 import org.bouncycastle.crypto.params.DHParameters;
00018 import org.bouncycastle.crypto.params.DHPublicKeyParameters;
00019 
00020 import edu.virtualschool.jco.JCOAsymmetricKey;
00021 import edu.virtualschool.jco.JCOBase64;
00022 import edu.virtualschool.jco.JCOEncodedBytes;
00023 import edu.virtualschool.jco.JCOFault;
00024 import edu.virtualschool.jco.JCOSignatureFault;
00025 import edu.virtualschool.jco.JCOSignedBytes;
00026 import edu.virtualschool.jwaa.RuntimeFault;
00027 
00028 
00034 public final class JCODHPublicKey extends JCOAsymmetricKey
00035 {
00036   private final BigInteger y;
00037 //  private final DHParameterSpec         dhSpec;
00038   private final DHParameters params;
00039   
00040   JCODHPublicKey(DHPublicKeyParameters key)
00041   {
00042       this.y = key.getY();
00043       this.params = key.getParameters();
00044 //      this.dhSpec = new DHParameterSpec(p.getP(), p.getG());
00045   }
00046   public JCODHPublicKey(String str) throws JCOFault
00047   {
00048      this(JCOBase64.decode(str)); 
00049   }
00050   public JCODHPublicKey(byte[] bytes) throws JCOFault
00051   {
00052     ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
00053     try
00054     {
00055       DERInputStream dis = new DERInputStream(bis);
00056       Object obj = dis.readObject();
00057       ASN1Sequence asn = (ASN1Sequence)obj;
00058       SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(asn);
00059       AlgorithmIdentifier algId = info.getAlgorithmId();
00060       if (algId.getObjectId().equals(X9ObjectIdentifiers.dhpublicnumber))
00061       {
00062         DHParameter p = new DHParameter((ASN1Sequence)algId.getParameters());
00063         DERInteger derY = null;
00064         try
00065         {
00066           derY = (DERInteger)info.getPublicKey();
00067         }
00068         catch (IOException e)
00069         {
00070           throw new IllegalArgumentException("invalid info structure in DH public key");
00071         }
00072         this.y = derY.getValue();
00073         this.params = new DHParameters(p.getP(), p.getG());
00074 //        if (params.getL() != null)
00075 //        {
00076 //          this.dhSpec = new DHParameterSpec(
00077 //              params.getP(),
00078 //              params.getG(),
00079 //              params.getL().intValue());
00080 //        }
00081 //        else
00082 //        {
00083 //          this.dhSpec = new DHParameterSpec(params.getP(), params.getG());
00084 //        }
00085       }
00086       else
00087         throw new RuntimeFault(algId + "  not a DH Key Agreement algorithm identifier ");
00088     }
00089     catch (IOException e)
00090     {
00091       throw new JCOFault(e, e);
00092     }
00093   }
00094 //X9ObjectIdentifiers.dhpublicnumber
00095 //  static PublicKey createPublicKeyFromPublicKeyInfo(
00096 //      SubjectPublicKeyInfo         info)
00097 //  {
00098 //      AlgorithmIdentifier     algId = info.getAlgorithmId();
00099 //        
00100 //      if (algId.getObjectId().equals(PKCSObjectIdentifiers.rsaEncryption)
00101 //        || algId.getObjectId().equals(X509ObjectIdentifiers.id_ea_rsa))
00102 //      {
00103 //            return new JCERSAPublicKey(info);
00104 //      }
00105 //      else if (algId.getObjectId().equals(PKCSObjectIdentifiers.dhKeyAgreement))
00106 //      {
00107 //            return new JCEDHPublicKey(info);
00108 //      }
00109 //      else if (algId.getObjectId().equals(X9ObjectIdentifiers.dhpublicnumber))
00110 //      {
00111 //            return new JCEDHPublicKey(info);
00112 //      }
00113 //      else if (algId.getObjectId().equals(OIWObjectIdentifiers.elGamalAlgorithm))
00114 //      {
00115 //            return new JCEElGamalPublicKey(info);
00116 //      }
00117 //      else if (algId.getObjectId().equals(X9ObjectIdentifiers.id_dsa))
00118 //      {
00119 //            return new JDKDSAPublicKey(info);
00120 //      }
00121 //      else if (algId.getObjectId().equals(OIWObjectIdentifiers.dsaWithSHA1))
00122 //      {
00123 //            return new JDKDSAPublicKey(info);
00124 //      }
00125 //      else if (algId.getObjectId().equals(X9ObjectIdentifiers.id_ecPublicKey))
00126 //      {
00127 //            return new JCEECPublicKey(info);
00128 //      }
00129 //      else
00130 //      {
00131 //          throw new RuntimeException("algorithm identifier in key not recognised");
00132 //      }
00133 //  }
00134 //  public JCODHPublicKey(RSAPublicKeySpec spec)
00135 //  {
00136 //      this.modulus = spec.getModulus();
00137 //      this.publicExponent = spec.getPublicExponent();
00138 //  }
00139 //  public JCODHPublicKey(RSAPublicKey key)
00140 //  {
00141 //      this.modulus = key.getModulus();
00142 //      this.publicExponent = key.getPublicExponent();
00143 //  }
00144 //  public JCODHPublicKey(SubjectPublicKeyInfo    info)
00145 //  {
00146 //      try
00147 //      {
00148 //          RSAPublicKeyStructure pubKey = new RSAPublicKeyStructure((ASN1Sequence)info.getPublicKey());
00149 //          this.modulus = pubKey.getModulus();
00150 //          this.publicExponent = pubKey.getPublicExponent();
00151 //  }
00152 //      catch (IOException e)
00153 //      {
00154 //        throw new IllegalArgumentException("invalid info structure in DH public key");
00155 //      }
00156 //  }
00157 //  static JCODHPublicKey createKey(KeySpec keySpec)
00158 //    throws InvalidKeySpecException
00159 //  {
00160 //    if (keySpec instanceof X509EncodedKeySpec)
00161 //        {
00162 //            try
00163 //            {
00164 //                return JDKKeyFactory.createPublicKeyFromDERStream(
00165 //                            new ByteArrayInputStream(((X509EncodedKeySpec)keySpec).getEncoded()));
00166 //            }
00167 //            catch (Exception e)
00168 //            {
00169 //                throw new InvalidKeySpecException(e.toString());
00170 //            }
00171 //        }
00172 //        else if (keySpec instanceof DHPublicKeySpec)
00173 //        {
00174 //            return new JCEDHPublicKey((DHPublicKeySpec)keySpec);
00175 //        }
00176 //    
00177 //        throw new InvalidKeySpecException("Unknown KeySpec type.");
00178 //    }
00179 //}
00180 //  {
00181 //    if (keySpec instanceof X509EncodedKeySpec)
00182 //    {
00183 //      X509EncodedKeySpec xks = (X509EncodedKeySpec)keySpec;
00184 //      byte[] bytes = xks.getEncoded();
00185 //      ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
00186 //      try
00187 //      {
00188 //        DERInputStream dis = new DERInputStream(bis);
00189 //        Object obj = dis.readObject();
00190 //        ASN1Sequence asn = (ASN1Sequence)obj;
00191 //        SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(asn);
00192 //        AlgorithmIdentifier  algId = info.getAlgorithmId();
00193 //        
00194 //        if (algId.getObjectId().equals(PKCSObjectIdentifiers.rsaEncryption)
00195 //          || algId.getObjectId().equals(X509ObjectIdentifiers.id_ea_rsa))
00196 //        {
00197 //              return new JCODHPublicKey(info);
00198 //        }
00199 //        else
00200 //        {
00201 //            throw new RuntimeException("algorithm identifier in key not RSA");
00202 //        }
00204 //      }
00205 //      catch (Exception e)
00206 //      {
00207 //        throw new InvalidKeySpecException(e.toString());
00208 //      }
00209 //    }
00210 //    else if (keySpec instanceof RSAPublicKeySpec)
00211 //    {
00212 //      return new JCODHPublicKey((RSAPublicKeySpec)keySpec);
00213 //    }
00214 //    throw new InvalidKeySpecException("Unknown KeySpec type.");
00215 //  }
00216   //
00217   // OAEP - public encrypt, private decrypt
00218   //
00219   public byte[] encryptBytes(byte[] inputBytes)
00220     throws JCOFault
00221   {
00222     throw new JCOFault("DH encryptBytes no implemented");
00223 //    AsymmetricBlockCipher cipher = new RSAEngine();
00224 //    OAEPEncoding engine = new OAEPEncoding(((PKCS1Encoding)cipher).getUnderlyingCipher());
00225 //    RSAKeyParameters key = new RSAKeyParameters(false, modulus, publicExponent);
00226 //    engine.init(true, key);
00227 //    try
00228 //    {
00229 //      return engine.processBlock(inputBytes, 0, inputBytes.length);
00230 //    }
00231 //    catch (Throwable e) { throw new JCOFault(e); }
00232   }
00233   //
00234   // PKCS1 - private encrypt, public decrypt
00235   //
00236   public byte[] decryptBytes(byte[] inputBytes)
00237     throws JCOFault
00238   {
00239     throw new JCOFault("DH decryptBytes no implemented");
00240 //    AsymmetricBlockCipher cipher = new RSAEngine();
00241 //    PKCS1Encoding engine = new PKCS1Encoding(((PKCS1Encoding)cipher).getUnderlyingCipher());
00242 //    RSAKeyParameters key = new RSAKeyParameters(false, modulus, publicExponent);
00243 //    engine.init(false, key);
00244 //    try
00245 //    {
00246 //      return engine.processBlock(inputBytes, 0, inputBytes.length);
00247 //    }
00248 //    catch (Throwable e) { throw new JCOFault(e); }
00249   }
00250   public final BigInteger getY() 
00251   {
00252     return y; 
00253   }
00254   public final String getAlgorithm()
00255   {
00256     return "DH"; 
00257   }
00258   public final String getFormat()
00259   {
00260     return "X.509";
00261   }
00262   public byte[] getEncoded()
00263   {
00264       ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
00265       DEROutputStream         dOut = new DEROutputStream(bOut);
00266       DHParameter dhp = new DHParameter(params.getP(), params.getG(), 0);
00267       AlgorithmIdentifier aid = new AlgorithmIdentifier(X9ObjectIdentifiers.dhpublicnumber, dhp.getDERObject());
00268       SubjectPublicKeyInfo    info = new SubjectPublicKeyInfo(aid, new DERInteger(y));
00269       try
00270       {
00271         dOut.writeObject(info);
00272         dOut.close();
00273         return bOut.toByteArray();
00274       }
00275       catch (IOException e)
00276       {
00277           throw new RuntimeFault(e, e);
00278       }
00279   }
00280   public JCOEncodedBytes encode() 
00281   {
00282     return new JCOEncodedBytes(getEncoded()); 
00283   }
00284   public Object verify(JCOSignedBytes signedObject) 
00285     throws JCOSignatureFault, JCOFault
00286   {
00287     return signedObject.verify(this);
00288   }
00289 //  public boolean equals(Object o)
00290 //  {
00291 //    if (!(o instanceof JCODHPublicKey))
00292 //      return false;
00293 //    if (o == this)
00294 //      return true;
00295 //    JCODHPublicKey key = (JCODHPublicKey) o;
00296 //    return getModulus().equals(key.getModulus())
00297 //      && getPublicExponent().equals(key.getPublicExponent());
00298 //  }
00299 //  public String toString()
00300 //  {
00301 //    StringBuffer buf = new StringBuffer();
00302 //    String nl = System.getProperty("line.separator");
00303 //    buf.append("PublicKey" + nl);
00304 //    buf.append("            modulus: " + this.getModulus().toString(16) + nl);
00305 //    buf.append("    public exponent: " + this.getPublicExponent().toString(16) + nl);
00306 //    return buf.toString();
00307 //  }
00308   public final AsymmetricKeyParameter getParameters()
00309   {
00310 //    DHParameters params = new DHParameters(dhSpec.getP(), dhSpec.getG());
00311     return new DHPublicKeyParameters(y, params);
00312   }
00313 }