001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package org.apache.geronimo.util.jce.provider;
020
021 import java.io.ByteArrayOutputStream;
022 import java.io.IOException;
023 import java.math.BigInteger;
024 import java.security.interfaces.DSAParams;
025 import java.security.interfaces.DSAPublicKey;
026 import java.security.spec.DSAParameterSpec;
027 import java.security.spec.DSAPublicKeySpec;
028
029 import org.apache.geronimo.util.asn1.ASN1Sequence;
030 import org.apache.geronimo.util.asn1.DERInteger;
031 import org.apache.geronimo.util.asn1.DEROutputStream;
032 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
033 import org.apache.geronimo.util.asn1.x509.DSAParameter;
034 import org.apache.geronimo.util.asn1.x509.SubjectPublicKeyInfo;
035 import org.apache.geronimo.util.asn1.x9.X9ObjectIdentifiers;
036 import org.apache.geronimo.util.crypto.params.DSAPublicKeyParameters;
037
038 public class JDKDSAPublicKey
039 implements DSAPublicKey
040 {
041 private BigInteger y;
042 private DSAParams dsaSpec;
043
044 JDKDSAPublicKey(
045 DSAPublicKeySpec spec)
046 {
047 this.y = spec.getY();
048 this.dsaSpec = new DSAParameterSpec(spec.getP(), spec.getQ(), spec.getG());
049 }
050
051 JDKDSAPublicKey(
052 DSAPublicKey key)
053 {
054 this.y = key.getY();
055 this.dsaSpec = key.getParams();
056 }
057
058 JDKDSAPublicKey(
059 DSAPublicKeyParameters params)
060 {
061 this.y = params.getY();
062 this.dsaSpec = new DSAParameterSpec(params.getParameters().getP(), params.getParameters().getQ(), params.getParameters().getG());
063 }
064
065 JDKDSAPublicKey(
066 BigInteger y,
067 DSAParameterSpec dsaSpec)
068 {
069 this.y = y;
070 this.dsaSpec = dsaSpec;
071 }
072
073 JDKDSAPublicKey(
074 SubjectPublicKeyInfo info)
075 {
076 DSAParameter params = new DSAParameter((ASN1Sequence)info.getAlgorithmId().getParameters());
077 DERInteger derY = null;
078
079 try
080 {
081 derY = (DERInteger)info.getPublicKey();
082 }
083 catch (IOException e)
084 {
085 throw new IllegalArgumentException("invalid info structure in DSA public key");
086 }
087
088 this.y = derY.getValue();
089 this.dsaSpec = new DSAParameterSpec(params.getP(), params.getQ(), params.getG());
090 }
091
092 public String getAlgorithm()
093 {
094 return "DSA";
095 }
096
097 public String getFormat()
098 {
099 return "X.509";
100 }
101
102 public byte[] getEncoded()
103 {
104 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
105 DEROutputStream dOut = new DEROutputStream(bOut);
106 SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, new DSAParameter(dsaSpec.getP(), dsaSpec.getQ(), dsaSpec.getG()).getDERObject()), new DERInteger(y));
107
108 try
109 {
110 dOut.writeObject(info);
111 dOut.close();
112 }
113 catch (IOException e)
114 {
115 throw new RuntimeException("Error encoding DSA public key");
116 }
117
118 return bOut.toByteArray();
119
120 }
121
122 public DSAParams getParams()
123 {
124 return dsaSpec;
125 }
126
127 public BigInteger getY()
128 {
129 return y;
130 }
131
132 public String toString()
133 {
134 StringBuffer buf = new StringBuffer();
135 String nl = System.getProperty("line.separator");
136
137 buf.append("DSA Public Key" + nl);
138 buf.append(" y: " + this.getY().toString(16) + nl);
139
140 return buf.toString();
141 }
142 }