1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.geronimo.util.jce.provider;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.math.BigInteger;
24 import java.security.interfaces.DSAParams;
25 import java.security.interfaces.DSAPublicKey;
26 import java.security.spec.DSAParameterSpec;
27 import java.security.spec.DSAPublicKeySpec;
28
29 import org.apache.geronimo.util.asn1.ASN1Sequence;
30 import org.apache.geronimo.util.asn1.DERInteger;
31 import org.apache.geronimo.util.asn1.DEROutputStream;
32 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
33 import org.apache.geronimo.util.asn1.x509.DSAParameter;
34 import org.apache.geronimo.util.asn1.x509.SubjectPublicKeyInfo;
35 import org.apache.geronimo.util.asn1.x9.X9ObjectIdentifiers;
36 import org.apache.geronimo.util.crypto.params.DSAPublicKeyParameters;
37
38 public class JDKDSAPublicKey
39 implements DSAPublicKey
40 {
41 private BigInteger y;
42 private DSAParams dsaSpec;
43
44 JDKDSAPublicKey(
45 DSAPublicKeySpec spec)
46 {
47 this.y = spec.getY();
48 this.dsaSpec = new DSAParameterSpec(spec.getP(), spec.getQ(), spec.getG());
49 }
50
51 JDKDSAPublicKey(
52 DSAPublicKey key)
53 {
54 this.y = key.getY();
55 this.dsaSpec = key.getParams();
56 }
57
58 JDKDSAPublicKey(
59 DSAPublicKeyParameters params)
60 {
61 this.y = params.getY();
62 this.dsaSpec = new DSAParameterSpec(params.getParameters().getP(), params.getParameters().getQ(), params.getParameters().getG());
63 }
64
65 JDKDSAPublicKey(
66 BigInteger y,
67 DSAParameterSpec dsaSpec)
68 {
69 this.y = y;
70 this.dsaSpec = dsaSpec;
71 }
72
73 JDKDSAPublicKey(
74 SubjectPublicKeyInfo info)
75 {
76 DSAParameter params = new DSAParameter((ASN1Sequence)info.getAlgorithmId().getParameters());
77 DERInteger derY = null;
78
79 try
80 {
81 derY = (DERInteger)info.getPublicKey();
82 }
83 catch (IOException e)
84 {
85 throw new IllegalArgumentException("invalid info structure in DSA public key");
86 }
87
88 this.y = derY.getValue();
89 this.dsaSpec = new DSAParameterSpec(params.getP(), params.getQ(), params.getG());
90 }
91
92 public String getAlgorithm()
93 {
94 return "DSA";
95 }
96
97 public String getFormat()
98 {
99 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 }