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