1 17 18 package org.apache.geronimo.util.jce.provider; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.math.BigInteger ; 23 import java.security.interfaces.DSAParams ; 24 import java.security.interfaces.DSAPublicKey ; 25 import java.security.spec.DSAParameterSpec ; 26 import java.security.spec.DSAPublicKeySpec ; 27 28 import org.apache.geronimo.util.asn1.ASN1Sequence; 29 import org.apache.geronimo.util.asn1.DERInteger; 30 import org.apache.geronimo.util.asn1.DEROutputStream; 31 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier; 32 import org.apache.geronimo.util.asn1.x509.DSAParameter; 33 import org.apache.geronimo.util.asn1.x509.SubjectPublicKeyInfo; 34 import org.apache.geronimo.util.asn1.x9.X9ObjectIdentifiers; 35 import org.apache.geronimo.util.crypto.params.DSAPublicKeyParameters; 36 37 public class JDKDSAPublicKey 38 implements DSAPublicKey 39 { 40 private BigInteger y; 41 private DSAParams dsaSpec; 42 43 JDKDSAPublicKey( 44 DSAPublicKeySpec spec) 45 { 46 this.y = spec.getY(); 47 this.dsaSpec = new DSAParameterSpec (spec.getP(), spec.getQ(), spec.getG()); 48 } 49 50 JDKDSAPublicKey( 51 DSAPublicKey key) 52 { 53 this.y = key.getY(); 54 this.dsaSpec = key.getParams(); 55 } 56 57 JDKDSAPublicKey( 58 DSAPublicKeyParameters params) 59 { 60 this.y = params.getY(); 61 this.dsaSpec = new DSAParameterSpec (params.getParameters().getP(), params.getParameters().getQ(), params.getParameters().getG()); 62 } 63 64 JDKDSAPublicKey( 65 BigInteger y, 66 DSAParameterSpec dsaSpec) 67 { 68 this.y = y; 69 this.dsaSpec = dsaSpec; 70 } 71 72 JDKDSAPublicKey( 73 SubjectPublicKeyInfo info) 74 { 75 DSAParameter params = new DSAParameter((ASN1Sequence)info.getAlgorithmId().getParameters()); 76 DERInteger derY = null; 77 78 try 79 { 80 derY = (DERInteger)info.getPublicKey(); 81 } 82 catch (IOException e) 83 { 84 throw new IllegalArgumentException ("invalid info structure in DSA public key"); 85 } 86 87 this.y = derY.getValue(); 88 this.dsaSpec = new DSAParameterSpec (params.getP(), params.getQ(), params.getG()); 89 } 90 91 public String getAlgorithm() 92 { 93 return "DSA"; 94 } 95 96 public String getFormat() 97 { 98 return "X.509"; 99 } 100 101 public byte[] getEncoded() 102 { 103 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 104 DEROutputStream dOut = new DEROutputStream(bOut); 105 SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, new DSAParameter(dsaSpec.getP(), dsaSpec.getQ(), dsaSpec.getG()).getDERObject()), new DERInteger(y)); 106 107 try 108 { 109 dOut.writeObject(info); 110 dOut.close(); 111 } 112 catch (IOException e) 113 { 114 throw new RuntimeException ("Error encoding DSA public key"); 115 } 116 117 return bOut.toByteArray(); 118 119 } 120 121 public DSAParams getParams() 122 { 123 return dsaSpec; 124 } 125 126 public BigInteger getY() 127 { 128 return y; 129 } 130 131 public String toString() 132 { 133 StringBuffer buf = new StringBuffer (); 134 String nl = System.getProperty("line.separator"); 135 136 buf.append("DSA Public Key" + nl); 137 buf.append(" y: " + this.getY().toString(16) + nl); 138 139 return buf.toString(); 140 } 141 } 142 | Popular Tags |