1 17 18 package org.apache.geronimo.util.jce.provider; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.io.ObjectInputStream ; 23 import java.io.ObjectOutputStream ; 24 import java.math.BigInteger ; 25 26 import javax.crypto.interfaces.DHPublicKey; 27 import javax.crypto.spec.DHParameterSpec; 28 import javax.crypto.spec.DHPublicKeySpec; 29 30 import org.apache.geronimo.util.asn1.ASN1Sequence; 31 import org.apache.geronimo.util.asn1.DERInteger; 32 import org.apache.geronimo.util.asn1.DEROutputStream; 33 import org.apache.geronimo.util.asn1.pkcs.DHParameter; 34 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier; 35 import org.apache.geronimo.util.asn1.x509.SubjectPublicKeyInfo; 36 import org.apache.geronimo.util.asn1.x9.X9ObjectIdentifiers; 37 import org.apache.geronimo.util.crypto.params.DHPublicKeyParameters; 38 39 public class JCEDHPublicKey 40 implements DHPublicKey 41 { 42 private BigInteger y; 43 private DHParameterSpec dhSpec; 44 45 JCEDHPublicKey( 46 DHPublicKeySpec spec) 47 { 48 this.y = spec.getY(); 49 this.dhSpec = new DHParameterSpec(spec.getP(), spec.getG()); 50 } 51 52 JCEDHPublicKey( 53 DHPublicKey key) 54 { 55 this.y = key.getY(); 56 this.dhSpec = key.getParams(); 57 } 58 59 JCEDHPublicKey( 60 DHPublicKeyParameters params) 61 { 62 this.y = params.getY(); 63 this.dhSpec = new DHParameterSpec(params.getParameters().getP(), params.getParameters().getG(), 0); 64 } 65 66 JCEDHPublicKey( 67 BigInteger y, 68 DHParameterSpec dhSpec) 69 { 70 this.y = y; 71 this.dhSpec = dhSpec; 72 } 73 74 JCEDHPublicKey( 75 SubjectPublicKeyInfo info) 76 { 77 DHParameter params = new DHParameter((ASN1Sequence)info.getAlgorithmId().getParameters()); 78 DERInteger derY = null; 79 80 try 81 { 82 derY = (DERInteger)info.getPublicKey(); 83 } 84 catch (IOException e) 85 { 86 throw new IllegalArgumentException ("invalid info structure in DH public key"); 87 } 88 89 this.y = derY.getValue(); 90 if (params.getL() != null) 91 { 92 this.dhSpec = new DHParameterSpec(params.getP(), params.getG(), params.getL().intValue()); 93 } 94 else 95 { 96 this.dhSpec = new DHParameterSpec(params.getP(), params.getG()); 97 } 98 } 99 100 public String getAlgorithm() 101 { 102 return "DH"; 103 } 104 105 public String getFormat() 106 { 107 return "X.509"; 108 } 109 110 public byte[] getEncoded() 111 { 112 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 113 DEROutputStream dOut = new DEROutputStream(bOut); 114 SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.dhpublicnumber, new DHParameter(dhSpec.getP(), dhSpec.getG(), dhSpec.getL()).getDERObject()), new DERInteger(y)); 115 116 try 117 { 118 dOut.writeObject(info); 119 dOut.close(); 120 } 121 catch (IOException e) 122 { 123 throw new RuntimeException ("Error encoding DH public key"); 124 } 125 126 return bOut.toByteArray(); 127 128 } 129 130 public DHParameterSpec getParams() 131 { 132 return dhSpec; 133 } 134 135 public BigInteger getY() 136 { 137 return y; 138 } 139 140 private void readObject( 141 ObjectInputStream in) 142 throws IOException , ClassNotFoundException 143 { 144 this.y = (BigInteger )in.readObject(); 145 this.dhSpec = new DHParameterSpec((BigInteger )in.readObject(), (BigInteger )in.readObject(), in.readInt()); 146 } 147 148 private void writeObject( 149 ObjectOutputStream out) 150 throws IOException 151 { 152 out.writeObject(this.getY()); 153 out.writeObject(dhSpec.getP()); 154 out.writeObject(dhSpec.getG()); 155 out.writeInt(dhSpec.getL()); 156 } 157 } 158 | Popular Tags |